[英]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.