我正在尝试创建一个堆积条形图,但没有运气。不幸的是,我无法弄清楚如何发布数据框,并且是新用户,因此无法发布图像。我已经给出了下面数据的布局。我希望按位置绘制水龙头图,其中 x 作为.factor(Time),Y 作为 %,条形图需要按组配对并由每个 E、T、P 的平均值填充,显示标准误差条形。虽然我很幸运将单个值绘制为点,但我无法绘制为堆叠条形。感谢您的帮助。
Location Group Time Mean E Mean T Mean P SE E SE T SE P
Farm T 48 0.52 0.02 0.47 0.29 0.07 0.29
Farm C 48 0.37 0.03 0.61 0.28 0.09 0.28
Farm T 24 0.59 0.01 0.40 0.28 0.06 0.28
Farm C 24 0.56 0.01 0.43 0.29 0.05 0.29
Farm T 0.5 0.56 0.01 0.43 0.29 0.04 0.29
Farm C 0.5 0.35 0.01 0.64 0.28 0.05 0.28
Pristine T 48 0.46 0.03 0.52 0.29 0.10 0.29
Pristine C 48 0.43 0.02 0.55 0.29 0.08 0.29
Pristine T 24 0.43 0.02 0.55 0.29 0.08 0.29
Pristine C 24 0.26 0.04 0.71 0.25 0.11 0.26
Pristine T 0.5 0.52 0.03 0.45 0.29 0.09 0.29
Pristine C 0.5 0.33 0.03 0.65 0.27 0.09 0.28
最佳答案
尝试:
dput(dat)
structure(list(Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Farm", "Pristine"), class = "factor"),
Group = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L), .Label = c("C", "T"), class = "factor"), Time = c(48,
48, 24, 24, 0.5, 0.5, 48, 48, 24, 24, 0.5, 0.5), `Mean E` = c(0.52,
0.37, 0.59, 0.56, 0.56, 0.35, 0.46, 0.43, 0.43, 0.26, 0.52,
0.33), `Mean T` = c(0.02, 0.03, 0.01, 0.01, 0.01, 0.01, 0.03,
0.02, 0.02, 0.04, 0.03, 0.03), `Mean P` = c(0.47, 0.61, 0.4,
0.43, 0.43, 0.64, 0.52, 0.55, 0.55, 0.71, 0.45, 0.65), `SE E` = c(0.29,
0.28, 0.28, 0.29, 0.29, 0.28, 0.29, 0.29, 0.29, 0.25, 0.29,
0.27), `SE T` = c(0.07, 0.09, 0.06, 0.05, 0.04, 0.05, 0.1,
0.08, 0.08, 0.11, 0.09, 0.09), `SE P` = c(0.29, 0.28, 0.28,
0.29, 0.29, 0.28, 0.29, 0.29, 0.29, 0.26, 0.29, 0.28)), .Names = c("Location",
"Group", "Time", "Mean E", "Mean T", "Mean P", "SE E", "SE T",
"SE P"), class = "data.frame", row.names = c(NA, -12L))
# maybe there is an easier way to do this merge?
library(data.table)
dat_m1 <- setDT(melt(dat[,1:6],id=c('Group','Time','Location')))
dat_m2 <- setDT(melt(dat[,c(1:3,7:9)],id=c('Location','Time','Group')))
dat_m1$var_group <- sapply(as.character(dat_m1$variable),function(x) unlist(strsplit(x, ' '))[2])
dat_m2$var_group <- sapply(as.character(dat_m2$variable),function(x) unlist(strsplit(x, ' '))[2])
setkey(dat_m1,Time,Location,Group,var_group)
setkey(dat_m2,Time,Location,Group,var_group)
dat_m <- merge(dat_m1,dat_m2, allow.cartesian=TRUE)
# suggested alternative for clarity
mydodge <- position_dodge(width=0.8)
ggplot(dat_mm,aes(x=as.factor(Time),y=value.x, ymin=value.x-value.y,
ymax=value.x+value.y,fill=variable.x)) +
geom_bar(stat='identity', position=mydodge, width=0.7) +
geom_errorbar(position=mydodge,width=0.2,stat='identity') +
facet_grid(Location ~ Group)
关于R ggplot2 堆积条形图,x as.factor(Time), fill = c(Mean.E, Mean.T, Mean.P),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23423096/