且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在每个方面而不是整体上显示摘要行

更新时间:2023-12-04 12:14:04

由于您说过要在一个块中完成,请注意,在.的许多用法中,您可以在几何图形中使用它来引用原始的ggplot()的数据参数.因此,您可以在此处进行其他汇总以获取geom_vline的值.我也只是颠倒了geom_point中的美观,而不是使用coord_flip.

Because you said you wanted to do it in one block, note that among the many uses of . you can use it in geoms to refer to the original data argument to ggplot(). So here you can do an additional summarise to get the values for geom_vline. I also just reversed the aesthetics in geom_point instead of using coord_flip.

library(tidyverse)

mtcars %>%
  rownames_to_column("carmodel") %>%
  mutate(brand = substr(carmodel, 1, 4)) %>%
  group_by(brand, cyl) %>%
  summarize(avgmpg = mean(mpg)) %>%
  ggplot(aes(y=brand, x = avgmpg)) +
  geom_point() +
  geom_vline(
    data = . %>%
      group_by(cyl) %>%
      summarise(line = mean(avgmpg)),
    mapping = aes(xintercept = line)
    ) +
  facet_grid(cyl~., scales = "free_y")

reprex软件包(v0.2.0)于2018-06-21创建.

Created on 2018-06-21 by the reprex package (v0.2.0).