ggplot2 keep unused levels barplot

RGgplot2LegendLevels

R Problem Overview


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

R Solutions


Solution 1 - R

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.

Solution 2 - R

Does this do what you want?

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

enter image description here

Solution 3 - R

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:

enter image description here

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:

enter image description here

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

Solution 4 - R

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUlrikView Question on Stackoverflow
Solution 1 - RJarretinhaView Answer on Stackoverflow
Solution 2 - RsmilligView Answer on Stackoverflow
Solution 3 - RUlrikView Answer on Stackoverflow
Solution 4 - RYan ZhangView Answer on Stackoverflow