Home > Software design >  How do i reduce the transparency of color for one group in ggpubr?
How do i reduce the transparency of color for one group in ggpubr?

Time:07-27

How do I change the transparency of a group in ggpubr plots. example dataset

library(ggpubr)
# Load data
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)
head(df[, c("wt", "mpg", "cyl")], 3)

data is:

               wt    mpg    cyl
              <dbl> <dbl>   <fct>
Mazda RX4     2.620  21.0   6
Mazda RX4 Wag 2.875  21.0   6
Datsun 710    2.320  22.8   4

I tried to create the plot with regression line using following code:

ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line", add.params = list(color = "white", fill = "blue"),
          conf.int = TRUE, # Add confidence interval
          cor.coef = TRUE, # Add correlation coefficient
          cor.coeff.args = list(method = "pearson"),
          color = "cyl", shape = "cyl",
          palette = c("#FF0000", "#0000FF", "#00FF00"),
          ellipse = TRUE
         )
# ggsave('test.png', width=6, height=6, units="in", dpi=300)

It gave me an output as follows:

scatter_plot

I want to make red color more transparent from this figure.

CodePudding user response:

You can change the alpha by changing the ggplot object itself rather than changing the grobs after the plot is built. This keeps the legend consistent with the plot, and allows you to modify theme elements, scales etc

library(ggpubr)

data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)

p <- ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line", add.params = list(color = "white", fill = "blue"),
          conf.int = TRUE, # Add confidence interval
          cor.coef = TRUE, # Add correlation coefficient
          cor.coeff.args = list(method = "pearson"),
          color = "cyl", shape = "cyl",
          palette = c("#FF0000", "#0000FF", "#00FF00"),
          ellipse = TRUE
         )

p$layers[[3]]$aes_params <- list()
p$layers[[3]]$mapping <- aes(wt, mpg, color = cyl, fill = cyl, group = cyl,
                             alpha = cyl)
p <- p   scale_alpha_manual(values = c(0.02, 0.2, 0.2), name = "cyl")

p

enter image description here

CodePudding user response:

This is an interesting one. There is an ellipse.alpha argument which should allow you to assign a vector of values. However, if you create a column of alpha values with df$ellipse_alpha <- ifelse(df$cyl==4, 0.01, 0.2) and try to assign this to ellipse.alpha it throws an error:

`geom_smooth()` using formula 'y ~ x'
Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (156): alpha

I am not entirely sure where 156 comes from as the data is 32 rows. However, we can get around this by editing the underlying plot data with ggplot_build():

# Assign plot (same code as yours) to p
p  <- ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line", add.params = list(color = "white", fill = "blue"),
          conf.int = TRUE, # Add confidence interval
          cor.coef = TRUE, # Add correlation coefficient
          cor.coeff.args = list(method = "pearson"),
          color = "cyl", shape = "cyl",
          palette = c("#FF0000", "#0000FF", "#00FF00"),
          ellipse = TRUE
         )

# Look at each layer and find the one which is 
# stat_ellipse - in this case [[3]]
print(p$layers)
# [[3]]
# mapping: x = ~wt, y = ~mpg, colour = ~cyl, group = ~cyl, fill = ~cyl
# geom_polygon: na.rm = FALSE
# stat_ellipse: type = norm, level = 0.95, segments = 51, na.rm = FALSE
# position_identity

Once we know which layer it is we need to edit we can change the alpha of the underlying data:

# Get data
plot_data  <- ggplot_build(p)

# Edit the alpha in the desired group (1 in this case)
plot_data[["data"]][[3]]$alpha  <- ifelse(
    plot_data[["data"]][[3]]$group == 1, 
    0.02,
    0.2
)

# Convert to grob
grob  <- ggplot_gtable(plot_data)

# Draw grob as plot
as_ggplot(grob)

enter image description here

  • Related