79659721

Date: 2025-06-10 02:50:29
Score: 1.5
Natty:
Report link

When applying range limits to scale_y_*, this affects the data used itself. https://ggplot2.tidyverse.org/articles/faq-bars.html#axes-and-axis-limits

This might be the answer you're looking for.

library(ggplot2)

ggplot(
  data = economics,
  aes(x = date, y = pop)
) +
  geom_rect(
    mapping = aes(
      xmin = as.Date("1990", "%Y"),
      xmax = as.Date("2000", "%Y"),
      ymin = 1,
      ymax = 400000
    ),
    fill = "grey90",
    alpha = 0.25
  ) +
  geom_line() +
  theme_minimal() +
  scale_y_log10() +
  coord_cartesian(ylim = c(2e5, 4e5))

enter image description here

Reasons:
  • Probably link only (1):
  • Has code block (-0.5):
  • Starts with a question (0.5): When
  • Low reputation (0.5):
Posted by: Breeze