ggplot多个分组栏 [英] ggplot multiple grouping bar
问题描述
我想知道如何获得9条分组条形图(3x3)。
我的CSV: 这9个地块应根据类型A到I进行分组。 然后每个分组条形图的频率应该在y轴上,x轴按1到6 pce分组,并按年份分组。 我在Excel上有下面的例子(cf. image),并且想用ggplot在r上创建相同的结果。是否有可能? 谢谢 首先,将数据从宽转换为长格式。 接下来,以数字开头的变量名称,使用 现在使用 I would like to know how to get 9 grouping bar plot (3x3) togheter. My CSV : 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. Next, as during importing data letter X is added to variable names starting with number, remove it with Now use 这篇关于ggplot多个分组栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!数据< - read.csv(http://pastebin.com/raw.php?i=6pArn8GL,
$ b $ p
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)
data <- read.csv("http://pastebin.com/raw.php?i=6pArn8GL", sep = ";")
library(reshape2)
df.long<-melt(df,id.vars=c("ID","Type","Annee"))
substring()
.df.long$variable<-substring(df.long$variable,2)
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)