ggplot2 keep unused levels barplot

半城伤御伤魂 提交于 2019-11-26 04:51:01

问题


I want to plot unused levels (that is, levels where the count is 0) in my bar-plot, however, unused levels are dropped and I cannot figure out how to keep them

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()

In the above example, I want to see C plotted with a count of 0, but it is completely absent...

Thanks for any help Ulrik

Edit:

This does what I want

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\")

Guess the solution is to calculate the frequencies using table() and then plot


回答1:


You need to set drop=FALSE on both scales (fill and x) like this:

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)

Edit:

I'm pretty sure this works. Forgot to change x to type instead of group and the position='dodge'! Just paste and test. The stat_bin deals with bins with zero counts. Check the docs.




回答2:


Does this do what you want?

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




回答3:


Dropping levels does not work. Dropping levels on the first example

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)

results in this plot:

The solution is in second example where the frequencies are manually calculated:

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")

Results in this:

The last one is the most informative as space is taken up by the categories there count = 0




回答4:


you can also use "scale_fill_color" for example :

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)


来源:https://stackoverflow.com/questions/10834382/ggplot2-keep-unused-levels-barplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!