I'm trying to fill the area between a line and 0 but this doesn't work as the fill area "overflows" the line:
library(ggplot2)
# Create some dummy data
x <- c(1, 2, 3, 4, 5)
y <- c(1, -2, 3, -4, 5)
# Create a data frame with the x and y values
df <- data.frame(x, y)
# Create the line chart
ggplot(df, aes(x, y)) +
geom_line() +
# Fill the area above the line with a semi-transparent red color
# when y is more than 0
geom_ribbon(data=df, aes(ymin=0, ymax=ifelse(y >= 0, y, 0)), fill = "red", alpha=0.5)
Any ideas on how to make this work?
This solution creates a helper dataframe with added rows for every point the line crosses the x axis. Including these points in your ribbon will make it align correctly with the line.
library(ggplot2)
library(dplyr)
library(purrr)
x_cross <- df %>%
transmute(
x = pmap_dbl(
list(x, lag(x), y, lag(y)),
possibly(
\(x, xlag, y, ylag) approx(c(ylag, y), c(xlag, x), xout = 0)[[2]],
NA_real_
)
),
y = 0
) %>%
filter(!is.na(x)) %>%
bind_rows(df) %>%
mutate(y = pmax(y, 0))
ggplot(df, aes(x, y)) +
geom_line() +
geom_ribbon(data = x_cross, aes(x, y, ymin = 0, ymax = y), fill = "red", alpha = 0.5)