首页 > 解决方案 > ggplot2 - 只为一个几何设置限制

问题描述

可以申请xlim还是ylim只申请一个geom_

在伪代码中它会是这样的:

library(tidyverse)
cars %>%
ggplot() +
geom_point(aes(...) + xlim(0, 120) + ylim(0, 30)) +
annotate(geom = "point", x = 124, y = 35) 

标签: rggplot2

解决方案


您可以通过限制它的 DF 来限制一个 geom,如下所示:

library(tidyverse)

ggplot(cars) +
  geom_line(aes(speed, dist)) +
  geom_point(
    data = cars %>% filter(speed > 10 & speed < 20),
    aes(speed, dist)
  )


推荐阅读