R中的多变量分组条形图,具有均值和sd

时间:2015-09-01 13:37:07

标签: r

我有一个大型数据框,基本上是这样的:

m.percent   nm.percent  sw.percent  x.percent   conc
78,60           21,4    39,3        39,3           1 
56,90           43,1    35,8        21,1           1
49,30           50,7    13,3        36,0           1
55,00           45,0    30,0        25,0           1
40,20           59,8    26,2        14,0          10
59,50           40,5    31,3        28,2          10
60,60           39,4    31,8        28,8          10
68,70           31,3    43,3        25,4          10
86,80           13,2    39,7        47,1          10
38,30           61,7    19,1        19,1          50
47,60           52,4    23,2        24,4          50
59,80           40,2    30,8        29,0          50

我想根据浓度,根据每个百分比列的平均值(+/- sd)创建一个分组条形图。 例如:x轴应为1,10,50,并且对于这些值中的每一个,应该有四个柱(m。%的平均值,nm。%的平均值,sw%的平均值,x.percent的平均值) )。
我真的不知道从哪里开始,我发现其他例子只有3列,没有事先计算(均值和标准差)。

1 个答案:

答案 0 :(得分:0)

查看我在下面使用的库,他们帮助了很多。 这是你可以很容易找到的东西。 http://docs.ggplot2.org/current/

https://cran.r-project.org/web/packages/reshape2/reshape2.pdf

https://cran.r-project.org/package=data.table

#load libraries
#install them if you havent
library(ggplot2)
library(reshape2)
library(data.table)

#me making my own matrx
 a<-matrix(rnorm(50,50,5),10,5 )
 a<-as.data.frame(a)
 a[,6] <-  c(1,1,1,10,10,10,50,50,50,50)
 colnames(a) <- c("m.percent",   "nm.percent",  "sw.percent", "x.percent"  ,"extraperc","conc")

#plug your matrix in as a
data<-melt(a, id.vars="conc")
data<-as.data.table(data)
data.m<- data[,mean(value), by=list(conc, variable)]
ggplot(data.m, aes(x=conc, y=data.m$V1, fill=variable)) + geom_bar(position=position_dodge(), stat="identity")
相关问题