Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

r - ggplot multiple grouping bar

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...