繁体   English   中英

R中的多元分组条形图,均值和标准差

[英]multivariate grouped bar plot in R with mean and sd

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

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.percent 的平均值、nm.percent 的平均值、sw.percent 的平均值、x.percent 的平均值) )。
我真的不知道从哪里开始,我发现的其他例子只有 3 列并且没有事先计算(平均值和标准差)。

查看我在下面使用的库,它们提供了大量帮助。 这是您可以轻松查找的内容。 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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM