ggplot绘制问题和错误栏

时间:2016-04-20 20:54:30

标签: r ggplot2

所以我有一些使用read.csv导入R的数据。

d = read.csv("Flux_test_results_for_R.csv", header=TRUE)
rows_to_plot = c(1,2,3,4,5,6,13,14)
d[rows_to_plot,]

看起来效果很好:

> d[rows_to_plot,]
        strain selective        rate      ci.low     ci.high
1         4051       rif 1.97539e-09 6.93021e-10 5.63066e-09
2         4052       rif 2.33927e-09 9.92957e-10 5.51099e-09
3  4081 (mutS)       rif 1.32915e-07 1.05363e-07 1.67671e-07
4  4086 (mutS)       rif 1.80342e-07 1.49870e-07 2.17011e-07
5  4124 (mutL)       rif 5.53369e-08 4.03940e-08 7.58077e-08
6  4125 (mutL)       rif 1.42575e-07 1.14957e-07 1.76828e-07
13    4760-all       rif 6.74928e-08 5.41247e-08 8.41627e-08
14    4761-all       rif 2.49119e-08 1.91979e-08 3.23265e-08

所以现在我试图绘制列“rate”,其中“strain”作为标签,ci.low和ci.high作为置信区间的边界。

使用ggplot,我甚至无法让情节发挥作用。这给出了一个图,其中所有点在y轴上都是1:

g <- ggplot(data=d[rows_to_plot,], aes(x=strain, y=rate))
g + geom_dotplot() 

尝试错误栏:

error_limits = aes(ymax = d2$ci.high, ymin = d2$ci.low)
g + geom_errorbar(error_limits)

你可以告诉我,我是一个完整的菜鸟在R中绘制东西,任何帮助都会受到赞赏。

回复更新 发生了两件事。根据我选择的boshek的答案,我似乎是geom_point(),而不是geom_dotplot()

另一个问题是,最初,我过滤了数据只绘制了一些行,但我也没有按行过滤错误限制。所以我转到:

d2 = d[c(1,2,3,4,5,6,13,14),]
error_limits = aes(ymax = d2$ci.high, ymin = d2$ci.low)
g = ggplot(data=d2, ...etc...

1 个答案:

答案 0 :(得分:1)

一些一般性评论。远离使用attach。虽然它有它的用途,但对于初学者来说它可能会非常混乱。习惯d$straind$selective之类的内容。也就是说,一旦用ggplot()调用数据框,您可以随后通过名称引用该数据框中的变量。另外,您真的需要用可重复的示例提问。这是了解如何在R中提问的一个非常重要的步骤。

现在为情节。我认为这应该有效:

error_limits = aes(ymax = rate + ci.high, ymin = rate - ci.low)

ggplot(data=d[rows_to_plot,], aes(x=strain, y=rate)) +
 geom_point() +
 geom_errorbar(error_limits)

但当然这是未经测试的,因为您还没有提供可重复的示例。

相关问题