【问题标题】:Different `geom_hline()` for each facet of ggplot [duplicate]ggplot的每个方面都有不同的`geom_hline()` [重复]
【发布时间】:2019-06-12 03:07:34
【问题描述】:

library(tidyverse)
ggplot(mpg, aes(cty, hwy)) + 
  geom_point() + 
  facet_grid(year ~ fl) + 
  geom_hline(yintercept = mean(mpg$hwy))

我希望上面显示的方面中的每个geom_hline() 都是仅包含在该方面中的点的平均值。我认为我可以用(如下)之类的东西来做到这一点。但这不起作用。我很接近,对吧?

library(tidyverse)
ggplot(mpg, aes(cty, hwy)) + 
  geom_point() + 
  facet_grid(year ~ fl) + 
  geom_hline(yintercept = mean(mpg %>% group_by(year, fl)$hwy))

【问题讨论】:

    标签: r ggplot2 dplyr


    【解决方案1】:

    如果您希望将每个构面用作数据框中的列的值,并且该值在每个构面中是唯一的,那么您可以使用 geom_hline(aes(yintercept=column)),然后绘制每个方面都有一条水平线

    【讨论】:

    猜你喜欢
    • 2021-10-02
    • 2019-01-17
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多