Suppose I have some data that looks like this:
library(tidyverse)
df <- tibble(x = c(0, 2, 5),
y = c(1000, 1, 1))
df |>
ggplot(aes(x,y)) +
geom_line()
I want to zoom in on a region based on the x
value. But when I try to use coord_cartesian
like this
df |>
ggplot(aes(x,y)) +
geom_line() +
coord_cartesian(xlim = c(1.5, NA))
the y
axis does not automatically adjust. It gives a lot of empty space like this
If I filter
the data before plotting, then I’ll lose the meaningful diagonal segment. This image needs to be automatically generated, so I cannot manually specify the ylim
in coord_cartesian
.