ggplot的每个方面都有不同的`geom_hline()` [英] Different `geom_hline()` for each facet of ggplot
本文介绍了ggplot的每个方面都有不同的`geom_hline()`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl) +
geom_hline(yintercept = mean(mpg$hwy))
我希望上面显示的方面中的每个 geom_hline()
是仅包含在该方面中的点的均值.我认为我可以用下面的方法做到这一点.但这是行不通的.我很近吧?
I want each geom_hline()
in the facet shown above to be the mean of the points that are only contained within that facet. I would think that I could do it with something like (below). But that doesn't work. I'm close, right?
library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl) +
geom_hline(yintercept = mean(mpg %>% group_by(year, fl)$hwy))
推荐答案
如果您希望将每个构面的值都用作数据框中的列,并且该值在每个构面内都是唯一的,则可以使用geom_hline(aes(yintercept = column)),然后将为每个构面绘制一条水平线
If you have the value you wish to use for each facet as a column in the data frame, and that value is unique within each facet, then you can use geom_hline(aes(yintercept=column)), which will then plot a horizontal line for each of the facets
这篇关于ggplot的每个方面都有不同的`geom_hline()`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文