我正在ggplot2中制作一个躲闪的条形图,一个分组的数量为零,我想显示.我记得有一段时间在这里看到这个,并认为它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
Run Code Online (Sandbox Code Playgroud)
San*_*att 16
更新 geom_bar()需求stat = "identity"
值得一提的是:上面的计数表dat包含NA.有时候,有一个明确的0代替它是有用的; 例如,如果下一步是将计数放在柱上方.下面的代码就是这样做的,虽然它可能并不比Joran简单.它包括两个步骤:使用计数器进行交叉制表dcast,然后使用熔化表melt,然后ggplot()照常进行.
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)
Run Code Online (Sandbox Code Playgroud)

S_B*_*BRT 15
以下是您无需先创建汇总表的方法.
它在我的CRAN版本(2.2.1)中不起作用,但在ggplot(2.2.1.900)的最新开发版本中,我没有任何问题.
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
geom_bar(position = position_dodge(preserve = "single"))
Run Code Online (Sandbox Code Playgroud)
http://ggplot2.tidyverse.org/reference/position_dodge.html
jor*_*ran 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")
Run Code Online (Sandbox Code Playgroud)

我认为使用stat_bin(drop = FALSE,geom = "bar",...)相反会起作用,但显然它没有.
我问了同样的问题,但我只想使用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"
Run Code Online (Sandbox Code Playgroud)
这是产生图形的调用
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)
Run Code Online (Sandbox Code Playgroud)
它产生了这个图:

此外,如果存在连续数据,例如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]
Run Code Online (Sandbox Code Playgroud)
然后使用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")
Run Code Online (Sandbox Code Playgroud)
给我们这张图:

| 归档时间: |
|
| 查看次数: |
14098 次 |
| 最近记录: |