接待员:面ggplotbarplot多个dataframe科勒姆 - R: Faceted ggplot barplot over multiple colums of dataframe

- 此内容更新于:2016-02-24
主题:

我有以下数据帧:我相信这不是一个完美的实现,我打开建议如何可以做得更好:)我怎么能创建一个面为一个条形图,B和C使用ggplot?这barplot将显示3barplots(一个用于每一列dataframe)和各有4条里面,NA,少,平均等等。我不确定的部分呢???但希望下面的代码块帮助…提前谢谢!最好,拉尔夫

原文:

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

解决方案:
你需要你的数据,即把它分为长格式:编辑:关于未使用的因素水平又融化后你必须分配水平:即使你用融化的参数未使用的因素水平下降而融化。此外,你必须增加你的阴谋,解释barplotggplot2保持未使用的水平:
原文:

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