Is there any way of having the alpha value vary across an area chart in R ggplot?
I'd like to vary the alpha across x so that it's dark on the right and light on the left.
I'd also be interested in having the alpha value be based on the y value, so it's darkest at the peak and lightest at the trough.
Any ideas?
Here's my code:
library(ggplot2)
x <- 1:1000
y <- sin(2*pi*x/1000 + 4)*25 + 45
df <- data.frame(x, y)
df$month <- rep("", times = nrow(df))
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
"Dec")
for (i in 1:nrow(df)){
ind <- floor(i/83.33334) + 1
print(ind)
df$names[i] <- months[ind]
}
df$names <- factor(df$names, levels = months)
p <- ggplot(df, aes(x = x, y = y, fill = "red"))+
geom_area(position = "identity", aes(alpha = x/1000))+
theme_minimal() +
theme(legend.position = "none")+
scale_alpha_continuous(guide=FALSE)
p
EDIT: Crucially, unlike other questions that this question is (not even remotely) similar to, I am interested in responses specifically for geom_area style ggplots.
It is important that this question stay open so that those with the same question as myself know workarounds, using geom_segment, or know that what is being asked is not possible within ggplot. So whoever closed my question can just bloody well open it again and stop gate-keeping this site.
FURTHER EDIT: This question now has a whole new answer that is not in the supposed "duplicates" which is to use geom_col. Works a treat. The people deserve to know! Un-close this question!