是否可以在“逐列”的基础上在ggplot条形图上绘制预先计算值的误差条?
鉴于以下内容:
require(ggplot2)
library("ggplot2")
require(reshape2)
library("reshape2")
df <- structure(list(PVC1 = 0.4019026, PVC2 = 0.4479259, PVC3 = 0.4494118, PVC4 = 0.4729437,
PVC5 = 0.4800556, PVC6 = 0.449229, PVC7 = 0.4905295, PVC8 = 0.4457566,
PVC9 = 0.4271259, PVC10 = 0.4850341, PVC11 = 0.4369965, PVC12 = 0.4064052,
PVC13 = 0.3743776, PVC14 = 0.3603853, PVC15 = 0.3965469, PVC16 = 0.365461),
.Names = c("PVC1","PVC2","PVC3","PVC4","PVC5","PVC6","PVC7","PVC8",
"PVC9","PVC10","PVC11","PVC12","PVC13","PVC14","PVC15","PVC16"),
class = "data.frame",
row.names = c(NA, -1L)
)
melted_df <- melt(df, variable.name = "Locus", value.name = "GC")
st_dev <- c(0.023031363, 0.024919217, 0.017371129,
0.019008759, 0.026650605, 0.026904926,
0.024227542, 0.017767553, 0.026152478,
0.039770898, 0.023929714, 0.028845442,
0.015572219, 0.024967336, 0.014955416, 0.024569096)
gc_chart <- ggplot(melted_df, aes(Locus, GC*100, fill=Locus,)) +
geom_bar(stat = "identity")
gc_chart <- gc_chart + ylab("GC Content (%)")
gc_chart <- gc_chart + theme(axis.text.x = element_text(angle=45, hjust=1))
gc_chart <- gc_chart + geom_abline(intercept=40.8891366,
slope=0,
colour="blue",
linetype="dashed")
gc_chart <- gc_chart + coord_cartesian(ylim=c(0,60))
gc_chart
产生这个条形图:
我希望能够在图中相应的列上绘制我预先计算的st devs的每个值。
这可以通过提供geom_errorbar
向量来完成吗?或者是否可能更容易首先在数据框中包含st dev信息?
(顺便说一句,我没有得到R来计算st dev本身的原因是我已经在图上绘制了平均值,所以R没有数据来计算它)
答案 0 :(得分:1)
我认为如果可能的话,它是不值得推荐的。特别是当您需要melted_df
的GC值来计算ymin
和ymax
值时。但是如果由于某种原因想要避免修改melted_df
,你总是可以这样做:
gc_chart +
geom_errorbar(aes(Locus,
ymin = (GC - cbind(melted_df, st_dev)$st_dev)*100,
ymax = (GC + cbind(melted_df, st_dev)$st_dev)*100))