Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot alpha = 0 not working

Tags:

r

ggplot2

I am trying to replicate a trajectory plot of some longitudinal data with highlighting of random subject trajectories by adjusting the alpha value. I am 95% of the way there except I can't seem to get complete transparency of the line plots when alpha = 0 (it still appears grey). Any ideas? - the data is here:

https://d.pr/f/iIoE8q+

Thank you.

# Assign random alpha (0 [90%] or 1 [10%]) values
macs <-  ddply(macs, .(id), function(x){
  x$alpha = ifelse(runif(n = 1) > 0.9, 1, 0)
  x
})

# Loess fit
fit <- loess(CD4 ~ time, data=macs, span = 0.5)

# Plot
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)) + 
  guides(alpha=FALSE)

plot to replicate

my attempt

like image 781
LucaS Avatar asked Mar 13 '18 02:03

LucaS


People also ask

How do I fix ggplot2 that doesn't work?

If the previous fixes don’t work, you may need to remove the current version of ggplot2 completely and re-install it: #remove ggplot2 remove.packages ("ggplot2") #install ggplot2 install.packages ("ggplot2") #load ggplot2 library(ggplot2) #create scatterplot of x vs. y ggplot (df, aes (x=x, y=y)) + geom_point ()

What does “could not find function “ggplot”” mean?

Error in ggplot(df, aes(x = x, y = y)) : could not find function "ggplot" This error occurs when you attempt to create a plot using the ggplot2 data visualization package, but have failed to load the package first. This tutorial explains five potential ways to fix this error. How to Reproduce this Error. Suppose we run the following code in R:

Is ‘ggplot’ available in R?

The graphs would run perfectly up until today, when I was met with this message: package ‘ggplot’ is not available (for R version 4.0.2) I have it installed and I have the library code as well. It just stopped working today.

How does the alpha value affect the smooth line?

The alpha value controls the opacity of the confidence interval, not the smooth line. Sorry, something went wrong. Sign up for free to subscribe to this conversation on GitHub .


2 Answers

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()

enter image description here

like image 186
neilfws Avatar answered Nov 15 '22 01:11

neilfws


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: enter image description here

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: enter image description here

like image 30
onlyphantom Avatar answered Nov 15 '22 00:11

onlyphantom