ggplot多个分组栏 [英] ggplot multiple grouping bar

查看:178
本文介绍了ggplot多个分组栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何获得9条分组条形图(3x3)。

我的CSV:数据< - read.csv(http://pastebin.com/raw.php?i=6pArn8GL,
$ b $ p

这9个地块应根据类型A到I进行分组。



然后每个分组条形图的频率应该在y轴上,x轴按1到6 pce分组,并按年份分组。

我在Excel上有下面的例子(cf. image),并且想用ggplot在r上创建相同的结果。是否有可能?



谢谢

解决方案

首先,将数据从宽转换为长格式。

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

接下来,以数字开头的变量名称,使用 substring()移除它。

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

现在使用变量为x,为y, Annee geom_bar()来获取barplot。使用 facet_wrap(),您可以按 Type 分割数据。

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


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

解决方案

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)

这篇关于ggplot多个分组栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆