46

ggplot2 でかわされた棒グラフを作成していますが、1 つのグループに表示したいカウントがゼロになっています。しばらく前にHEREでこれを見たことを思い出し、scale_x_discrete(drop=F)うまくいくだろうと考えました。覆い焼きバーでは機能しないようです。ゼロカウントを表示するにはどうすればよいですか?

たとえば、(下のコード) 下のプロットでは、type8~group4 には例がありません。バーを削除するのではなく、プロットにゼロカウントの空きスペースを表示したいと思います。これどうやってするの?

ここに画像の説明を入力

mtcars2 <- data.frame(type=factor(mtcars$cyl), 
    group=factor(mtcars$gear))

m2 <- ggplot(mtcars2, aes(x=type , fill=group))
p2 <- m2 + geom_bar(colour="black", position="dodge") +
        scale_x_discrete(drop=F)
p2
4

6 に答える 6

31

最初に集計テーブルを作成せずにそれを行う方法を次に示します。
私のCRANバージョン(2.2.1)では機能しませんでしたが、ggplotの最新開発バージョン(2.2.1.900)では問題はありませんでした。

ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
  geom_bar(position = position_dodge(preserve = "single"))

http://ggplot2.tidyverse.org/reference/position_dodge.html

于 2018-01-19T14:46:25.123 に答える
17

更新された geom_bar()ニーズstat = "identity"

価値があるのは、上記のカウントの表 dat には NA が含まれていることです。場合によっては、代わりに明示的な 0 を指定すると便利です。たとえば、次のステップがバーの上にカウントを配置することである場合。次のコードはまさにそれを行いますが、おそらく Joran のコードより単純ではありません。これには 2 つの手順が含まれdcastます。 meltggplot()

library(ggplot2)
library(reshape2)
mtcars2 = data.frame(type=factor(mtcars$cyl), group=factor(mtcars$gear))

dat = dcast(mtcars2, type ~ group, fun.aggregate = length)
dat.melt = melt(dat, id.vars = "type", measure.vars = c("3", "4", "5"))
dat.melt

ggplot(dat.melt, aes(x = type,y = value, fill = variable)) + 
  geom_bar(stat = "identity", colour = "black", position = position_dodge(width = .8), width = 0.7) +
  ylim(0, 14) +
  geom_text(aes(label = value), position = position_dodge(width = .8), vjust = -0.5)

ここに画像の説明を入力

于 2012-04-26T07:32:47.467 に答える
13

私が知っている唯一の方法は、カウントを事前に計算してダミー行を追加することです。

dat <- rbind(ddply(mtcars2,.(type,group),summarise,count = length(group)),c(8,4,NA))

ggplot(dat,aes(x = type,y = count,fill = group)) + 
    geom_bar(colour = "black",position = "dodge",stat = "identity")

ここに画像の説明を入力

代わりに使用するとうまくいくと思いましたstat_bin(drop = FALSE,geom = "bar",...)が、どうやらそうではありません。

于 2012-04-26T03:41:22.160 に答える
8

data.table私はこれと同じ質問をしましたが、はるかに大きなデータセットに対してより高速なソリューションであるため、のみを使用したかったのです。経験の浅い人や、自分がしたことの理由を理解したい人が簡単に理解できるように、データにメモを含めました。mtcarsデータセットを操作した方法は次のとおりです。

library(data.table)
library(scales)
library(ggplot2)

mtcars <- data.table(mtcars)
mtcars$Cylinders <- as.factor(mtcars$cyl) # Creates new column with data from cyl called Cylinders as a factor. This allows ggplot2 to automatically use the name "Cylinders" and recognize that it's a factor
mtcars$Gears <- as.factor(mtcars$gear) # Just like above, but with gears to Gears
setkey(mtcars, Cylinders, Gears) # Set key for 2 different columns
mtcars <- mtcars[CJ(unique(Cylinders), unique(Gears)), .N, allow.cartesian = TRUE] # Uses CJ to create a completed list of all unique combinations of Cylinders and Gears. Then counts how many of each combination there are and reports it in a column called "N"

そして、ここにグラフを生成した呼び出しがあります

ggplot(mtcars, aes(x=Cylinders, y = N, fill = Gears)) + 
               geom_bar(position="dodge", stat="identity") + 
               ylab("Count") + theme(legend.position="top") + 
               scale_x_discrete(drop = FALSE)

そして、次のグラフが生成されます。

円柱グラフ

さらに、diamondsデータセットのような連続データがある場合 (mnel に感謝):

library(data.table)
library(scales)
library(ggplot2)

diamonds <- data.table(diamonds) # I modified the diamonds data set in order to create gaps for illustrative purposes
setkey(diamonds, color, cut) 
diamonds[J("E",c("Fair","Good")), carat := 0]
diamonds[J("G",c("Premium","Good","Fair")), carat := 0]
diamonds[J("J",c("Very Good","Fair")), carat := 0]
diamonds <- diamonds[carat != 0]

次に、使用CJも同様に機能します。

data <- data.table(diamonds)[,list(mean_carat = mean(carat)), keyby = c('cut', 'color')] # This step defines our data set as the combinations of cut and color that exist and their means. However, the problem with this is that it doesn't have all combinations possible
data <- data[CJ(unique(cut),unique(color))] # This functions exactly the same way as it did in the discrete example. It creates a complete list of all possible unique combinations of cut and color
ggplot(data, aes(color, mean_carat, fill=cut)) +
             geom_bar(stat = "identity", position = "dodge") + 
             ylab("Mean Carat") + xlab("Color")

このグラフを与える:

ダイヤモンド固定

于 2014-05-21T00:53:59.807 に答える