ggplot alpha = 0 not working
If you're trying to reproduce the first chart, the x-axis should be time and the y-axis is the residuals of the fit. So you need to combine the original data macs
, containing time
, with the residuals in fit
. I would use broom::augment
for that.
For the alpha issue, you need to convert the values to factors then work with scale_alpha_discrete
.
library(tidyverse)
library(broom)
fit %>%
augment() %>%
bind_cols(macs) %>%
mutate(alpha = factor(alpha)) %>%
ggplot(aes(time, .resid)) +
geom_line(aes(group = id, alpha = alpha)) +
scale_alpha_discrete(range = c(0, 1)) +
geom_point(size = 0.5) +
guides(alpha = FALSE) +
theme_bw()
The problem has got to do with the color
behavior in ggplot
. A simple fix is to explicitly specify its color to be NA
when the corresponding alpha
is 0. Here's the code I would add before the ggplot
call:
color_rule <- ifelse(macs$alpha == 0, NA, "black")
And slight modifications to ggplot
:
ggplot(data = macs, aes(x = fitted(fit), y = resid(fit))) +
geom_point(aes(alpha=1), size = 0.5) +
geom_line(aes(alpha=alpha, group=id), color=color_rule) +
guides(alpha=FALSE)
This would get you the following plot:
A clean, fully transparent geom_line
by having color=NA
. Just for comparison, ff you remove the color=NA
argument from geom_line
, even with alpha=0
you'll get what is effectively the "default color" of the line: