RalfB
RalfB

Reputation: 583

R: Faceted ggplot barplot over multiple colums of dataframe

I have the following data frame:

df = data.frame(ID=c(1,2,3), A=c(0,1,1), B=c(0,2,1), C=c(0,2,2))
df[df=="0"]<-NA;
df[df=="1"]<-"Less";
df[df=="2"]<-"Average";
df[df=="3"]<-"More";
# reset the id row again --- i know its lame, is there a better way?
df$ID = c(1:3);
# turn to factors
df$A = factor(df$A, levels = c("NA", "Less", "Average", "More"));
df$B = factor(df$B, levels = c("NA", "Less", "Average", "More"));
df$C = factor(df$C, levels = c("NA", "Less", "Average", "More"));

I am sure this is not a perfect implementation, I am open for suggestions how this can be done better :)

How can I create a faceted bar diagram for A,B, and C using ggplot? This barplot would then show 3 barplots (one for each column of the dataframe) and each have 4 bars inside, for NA, Less, Average, and More. I am unsure about the parts with the ??? but hope that the code piece below helps...

ggplot(df$???, aes(value, fill=???)) + 
geom_bar(aes(y = (..count..)/totalNumber)) + facet_wrap(~???) + 
theme(
  axis.text.x = element_text(angle = 45, hjust = 1, face=2), 
  axis.text=element_text(size=12), axis.title=element_text(size=14,face="bold"),   
  panel.background = element_rect(fill = "white")
) +   
scale_fill_manual(values=c("blue4","steelblue2", "blue4","steelblue2",   
  "blue4","steelblue2")) + opts(legend.position = "none") +   
scale_y_continuous(breaks=seq(0, 1, 0.1), labels = percent_format()) + 
xlab("") + ylab("") + 
coord_flip();

Thanks in Advance!

Best, Ralf

Upvotes: 1

Views: 894

Answers (1)

Beasterfield
Beasterfield

Reputation: 7113

You need to melt your data, i.e. bring it into long format:

library("reshape2")
df.molten <- melt(df, id.vars="ID")
head(df.molten)
  ID variable   value
1  1        A    <NA>
2  2        A    Less
3  3        A    Less
4  1        B    <NA>
5  2        B Average
6  3        B    Less

ggplot( df.molten , aes( x = value )  )+ 
  geom_bar() +
  facet_wrap( "variable" )

enter image description here

Edit:

Concerning the unused factor level More you have to assign the levels after melting again:

df.molten$value <- factor( df.molten$value, levels = c("NA", "Less", "Average", "More") )

Even if you use melt's parameter value.factor = TRUE unused factor levels are dropped while melting. Additionally, you have to add

+ scale_x_discrete( drop = FALSE )

to your plot, as explained in ggplot2 keep unused levels barplot:

enter image description here

Upvotes: 2

Related Questions