Peter Houk
Peter Houk

Reputation: 107

geom_area ggplot fill above threshold with data subset

I read several posts on how to fill above geom_area plots using the geom_ribbon function, but none have also dealt with subsets of data.

Consider the following data and plot. I simply want to fill above a threshold, 25 in this example (y-axis), but also fill only within a subset of days within each month, in this case between days 2 and 12. In sum, both criteria must be met in order to fill, and I'm trying to get a smooth fill.

I can improve upon my graph below by using the approx function to interpolate a lot of points on my line, but it still does not handle my subset and connects fill lines between months.

library(ggplot2)
y = sample(1:50)
x = seq(as.Date("2011-12-30"), as.Date("2012-02-17"), by="days", origin="1970-01-01")
z = format(as.Date(x), "%d")
z=as.numeric(z)

df <- data.frame(x,y,z)

plot<-ggplot(df, aes(x=x, y=y)) +
  geom_area(fill="transparent") +
  geom_ribbon(data=subset(df, z>=2 & z<=12), aes(ymin=25, ymax=ifelse(y< 25,20, y)), fill = "red", alpha=0.5) +
  geom_line() +
  geom_hline(yintercept = 25, linetype="dashed") +
  labs(y="My data") +
  theme_bw(base_size = 22)

plot

Figure

Upvotes: 1

Views: 1393

Answers (1)

Wolfgang Arnold
Wolfgang Arnold

Reputation: 1252

The data=subset(df, z>=2 & z<=12) removes lines from the dataframe, so the data is 'lost' for geom_ribbon. Instead of subsetting an additional condition for the y-value may get you closer to what you want to achieve:

plot<-ggplot(df, aes(x=x, y=y)) +
  geom_area(fill="transparent") +
  geom_ribbon(data=df, aes(ymin=25, ymax=ifelse((z>=2 & z<=12), ifelse(y < 25, 20, y), 25)), fill = "red", alpha=0.5) +
  geom_line() +
  geom_hline(yintercept = 25, linetype="dashed") +
  labs(y="My data") +
  theme_bw(base_size = 22)

Upvotes: 2

Related Questions