117

我想在我的条形图中绘制未使用的级别(即计数为 0 的级别),但是,未使用的级别被删除了,我不知道如何保留它们

df <- data.frame(type=c("A", "A", "A", "B", "B"), group=rep("group1", 5))
df$type <- factor(df$type, levels=c("A","B", "C"))

ggplot(df, aes(x=group, fill=type)) + geom_bar()

在上面的例子中,我想看到 C 绘制的计数为 0,但它完全不存在......

感谢您的帮助乌尔里克

编辑:

这做我想要的

df <- data.frame(type=c("A", "A", "A", "B", "B"), group=rep("group1", 5))
df1 <- data.frame(type=c("A", "A", "A", "B", "B", "A", "A", "C", "B", "B"), group=c(rep("group1", 5),rep("group2", 5)))

df$type <- factor(df$type, levels=c("A","B", "C"))
df1$type <- factor(df1$type, levels=c("A","B", "C"))
df <- data.frame(table(df))

df1 <- data.frame(table(df1))

ggplot(df, aes(x=group, y=Freq, fill=type)) + geom_bar(position="dodge")
ggplot(df1, aes(x=group, y=Freq, fill=type)) + geom_bar(position="dodge")

猜猜解决方案是使用 table() 计算频率,然后绘制

4

4 回答 4

90

您需要在两个刻度(填充和 x)上设置 drop=FALSE,如下所示:

library(ggplot2)
df <- data.frame(type=c("A", "A", "A", "B", "B"), group=rep("group1", 5))
df1 <- data.frame(type=c("A", "A", "A", "B", "B", "A", "A", "C", "B", "B"), group=c(rep("group1", 5),rep("group2", 5)))
df$type <- factor(df$type, levels=c("A","B", "C"))
df1$type <- factor(df1$type, levels=c("A","B", "C"))

plt <-  ggplot(df, aes(x=type, fill=type)) + 
          geom_bar(position='dodge') + 
          scale_fill_discrete(drop=FALSE) +
          scale_x_discrete(drop=FALSE)
plt1 <- ggplot(df1, aes(x=type, fill=type)) + 
          geom_bar(position='dodge') + 
          scale_fill_discrete(drop=FALSE) +
          scale_x_discrete(drop=FALSE)

编辑:

我很确定这行得通。忘记将 x 更改为 type 而不是 group 和 position='dodge'!只需粘贴和测试。stat_bin 处理计数为零的 bin。检查文档

于 2014-04-02T20:23:59.577 回答
76

这是做你想做的吗?

ggplot(df, aes(x=type)) + geom_bar() + scale_x_discrete(drop=FALSE)

在此处输入图像描述

于 2012-05-31T14:31:43.333 回答
10

降低水平不起作用。在第一个示例中降低级别

library(ggplot2)

df <- data.frame(type=c("A", "A", "A", "B", "B"), group=rep("group1", 5))
df$type <- factor(df$type, levels=c("A","B", "C"))

ggplot(df, aes(x=group, fill=type)) + geom_bar(position="dodge") + scale_x_discrete(drop=FALSE) + scale_fill_discrete(drop=FALSE)

结果在这个图中:

在此处输入图像描述

解决方案是在第二个示例中手动计算频率:

df <- data.frame(type=c("A", "A", "A", "B", "B"), group=rep("group1", 5))
df1 <- data.frame(type=c("A", "A", "A", "B", "B", "A", "A", "C", "B", "B"), group=c(rep("group1", 5),rep("group2", 5)))

df$type <- factor(df$type, levels=c("A","B", "C"))
df1$type <- factor(df1$type, levels=c("A","B", "C"))

df <- data.frame(table(df))
df1 <- data.frame(table(df1))

df$plot = "A"
df1$plot = "B"

df <- rbind(df, df1)

ggplot(df, aes(x=group, y=Freq, fill=type)) + geom_bar(position="dodge", stat="identity") + facet_wrap( ~ plot, scales="free")

结果如下:

在此处输入图像描述

最后一个是信息量最大的,因为空间被其中的类别占用 count = 0

于 2014-04-03T07:52:50.263 回答
2

您也可以使用“scale_fill_color”,例如:

plt <- ggplot(df, aes(x=type, fill=type)) + geom_bar(position='dodge') + scale_x_discrete(drop=FALSE)+
scale_fill_manual(
  values = c(
    "#ff6666",
    "#cc9900",
    "#cc9900",
    ),drop=FALSE)
于 2017-05-06T09:24:34.330 回答