简体   繁体   中英

multivariate grouped bar plot in R with mean and sd

I have a large data frame which essentially looks like this:

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

I would like to create a grouped bar plot based on the mean averages (+/-sd) of each of the percent columns according to conc.
For example: the x-axis should be 1, 10, 50, and for each of these values there should be four bars (mean of m.percent, mean of nm.percent, mean of sw.percent, mean of x.percent).
I don't really know where to start and other examples I have found work with only 3 columns and without prior calculations (mean and sd).

Look into the libraries I use below they help out a ton. This is stuff you can look up really easily. 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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