使用facet_wrap高亮显示点-显示每个面中的所有点

ghhaqwfi  于 2023-04-09  发布在  其他
关注(0)|答案(2)|浏览(137)

这是一个使用mtcars数据集的图,其中一个水平的cyl用不同的颜色高亮显示,并在所有点的顶部重新绘制。

data %>%
  ggplot() +
  aes(
    x = wt,
    y = mpg
  ) +
  geom_point(
    colour = "grey"
  ) +
  geom_point(
    data = data %>% filter(cyl == 4),
    colour = "tomato"
  ) +
  theme_bw()

有没有一种方法,如果我要做一个像下面这样的facet_wrap,我可以像上面那样突出显示一个面中的每个点,而不仅仅是单独绘制?

data %>%
  ggplot() +
  aes(
    x = wt,
    y = mpg,
    colour = factor(cyl)
  ) +
  geom_point() +
  facet_wrap(
    ~ factor(cyl)
  ) +
  theme_bw()

dm7nw8vv

dm7nw8vv1#

您可以首先Map所有没有因子变量grey的点,然后基于cal为点着色,并使用scale_color_hue在每个面上设置因子颜色,如下所示:

library(tidyverse)
mtcars %>%
  ggplot() +
  aes(
    x = wt,
    y = mpg,
  ) +
  geom_point(data = mtcars %>%
               select(-cyl), colour = "grey") +
  geom_point(aes(color = factor(cyl))) +
  scale_color_hue() +
  facet_wrap(
    ~ factor(cyl)
  ) +
  theme_bw()

创建于2023-04-08使用reprex v2.0.2

dddzy1tm

dddzy1tm2#

这就是gghighlight包的用途

library(ggplot2)
library(gghighlight)

ggplot(mtcars, aes(x = wt, y = mpg, color = as.character(cyl))) +
  geom_point() +
  gghighlight() +
  facet_wrap(~cyl)

创建于2023-04-08使用reprex v2.0.2

相关问题