【问题标题】:Add horizontal lines, grouped by factor添加水平线,按因子分组
【发布时间】:2014-08-18 12:58:11
【问题描述】:

我有一个线图,按rank 因子分组:

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

我有额外的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

我想用这些平均值添加四条垂直线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)) 

【问题讨论】:

  • geom_hline(data = res.sum.df.mean, aes(yintercept = mean_succes_rate, colour=rank))? (作为评论发布,因为问题缺少用于测试的示例数据。)
  • 谢谢!这确实有效;)

标签: r plot ggplot2


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    相关资源
    最近更新 更多