ggplot multiple grouping bar

自古美人都是妖i 提交于 2019-12-17 10:29:29

问题


I would like to know how to get 9 grouping bar plot (3x3) togheter.

My CSV : data <- read.csv("http://pastebin.com/raw.php?i=6pArn8GL", sep = ";")

The 9 plots should be groupes according "Type" A to I.

Then each grouped bar plot should have the frequency on the y axis, the x axis is grouped by 1 pce to 6 pce and subdiveded by year.

I have the following example on Excel (cf. image) and would like to create the same result on r with ggplot. Is it possible?

Thanks


回答1:


First, reshape your data from wide to long format.

library(reshape2)
df.long<-melt(df,id.vars=c("ID","Type","Annee"))

Next, as during importing data letter X is added to variable names starting with number, remove it with substring().

df.long$variable<-substring(df.long$variable,2)

Now use variable as x, value as y, Annee for fill and geom_bar() to get barplot. With facet_wrap() you can split data by Type.

ggplot(df.long,aes(variable,value,fill=as.factor(Annee)))+
   geom_bar(position="dodge",stat="identity")+
   facet_wrap(~Type,nrow=3)




回答2:


Using @Didzis reshaped data , here a lattice version:

barchart(value~variable|Type,
         groups=Annee,data=df.long,layout=c(3,3),
         between=list(3,3),
         axis=axis.grid,
         auto.key=TRUE)



来源:https://stackoverflow.com/questions/17303573/ggplot-multiple-grouping-bar

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