WBM
WBM

Reputation: 129

How to inhibit geom_area in ggplot2 from changing my ylimits

I have the following figure in R, generated using the ggplot2 package which resulted in the following: enter image description here

The code to obtain this plot is:

df <- data.frame(value_x = c(10,20,30,40), value_y = c(89.3, 89.4, 89.60, 90.1))
myplot <- ggplot(data = df, aes(x = value_x, y = value_y)) +
  geom_point() +
  geom_line()
myplot

Now I want to fill the area under this curve, but still keep the y-axis scale. When I add geom_area(alpha = 0.40) to the code, the plot becomes the following:

enter image description here

As you can see, the area runs from 0 to the curve, which rescales the y-axis. How can I inhibit this from happening?

Upvotes: 0

Views: 323

Answers (1)

markus
markus

Reputation: 26353

I suggest the use of geom_ribbon which understands ymin and ymax aesthetics. Unlike geom_area which gives a continuous bar plot that starts at 0.

myplot +
  geom_ribbon(aes(ymin = min(value_y), ymax = value_y))

enter image description here

Upvotes: 2

Related Questions