r - 添加水平线,按因素分组

标签 r plot ggplot2

我有一个线条图,按排名因素分组:

ggplot(data=res.sum.df, aes(x=i_id, y=success_rate, colour = rank)) +
geom_line(size=1.5, aes(group=rank)) 

enter image description here

我有额外的data.frame对象,它定义每个排名中所有值的平均值:

> res.sum.df.mean
Source: local data frame [4 x 2]

  rank mean_succes_rate
1    1       0.16666667
2    2       0.13735450
3    3       0.13628500
4    4       0.05797931

我想添加四 strip 有这些平均值的垂直线yintercept,另外根据现有的线图例着色(分组)

我尝试添加一些aes参数和其他东西,但我的所有组合都失败了(线条没有着色):

  ggplot(data=res.sum.df, aes(x=i_id, y=success_rate, colour = rank)) +
  geom_line(size=1.5, aes(group=rank)) +   
  geom_hline(yintercept = res.sum.df.mean$mean_succes_rate, aes(colour=rank)) 

enter image description here

最佳答案

您需要在美学映射中指定截距:

geom_hline(data = res.sum.df.mean, 
           aes(yintercept = mean_succes_rate, colour=rank))

关于r - 添加水平线,按因素分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25363886/

相关文章:

r - 在 R 中处理东部标准时间 (EST) 和东部夏令时 (EDT)

r - 将箭头放置在R中现有的背景图片上

r - 如何在 R 中使用 ggplot 绘制绘图区域的 'outside'?

使用 ggplot2 reshape 数据以在 R 中绘制

r - 在ggplot中绘制二维函数

r - R中使用降雪的并行蒙特卡罗模拟

r - 当所有元素都具有相同的值时,热映射颜色

python - hvplot 和 Holoplot 之间的区别

r - 将文本添加到 R 中的水平条形图,y 轴以不同的比例?

替换丢失顺序的 data.frame 中的值