Home > Software engineering >  Plot of coefficients confidence intervals: transparency of point according to logical condition (o
Plot of coefficients confidence intervals: transparency of point according to logical condition (o

Time:07-27

I want to plot points confidence intervals (CI). The points and CI should follow a user-defined color palette, but in addition to that, the filling of the points should change following a logical condition. In more detail, I aim to plot the coefficients confidence intervals of multiple models (different response variables, but same set of explanatory variables). Therefore, I would like my coefficients to follow a color palette established by me: basically, coefficients relating to the same response should have the same color. In addition to following a user-defined color palette, I would like coefficients that overlap 0 to be transparent (or should I say, to have a transparent fill, but with color that outlines the circumference). The lines of the CI should remain unchanged (same color as the point, solid without transparency). I have tested a few options with a dummy data frame, trying to play with fill, transparency (alpha), or storing the color in the data frame itself, but so far I've been unable to achieve the desired result. Any help much appreciated!

library(tidyverse)

# user-defined color palette
response.manual.col.scale <- c("#A6761D", "#FC8D62")

# dummy data frame: coefficients   confidence intervals for two different models (different responses, same predictors)
df <- data.frame(response = c("r1", "r1", "r2", "r2"),
                 explanatory = c("var1", "var2", "var1", "var2"),
                 coef = c(1, 2, 1, 2),
                 lower = c(-1, 1, 0.5, -2),
                 upper = c(4, 3, 1.5, 6)) %>% 
  mutate(
    # variable storing whether the confidence interval overlaps with 0
    coef.zero = if_else((lower/upper) < 0, true = TRUE, false = FALSE),
    # variable storing the colour to be used for fill and colour
    resp.col = rep(c("#A6761D", "#FC8D62"), each = 2),
    resp.fill = if_else(coef.zero == TRUE, true = "black", false = resp.col))

ggplot(data = df,
       aes(x = explanatory,
           y = coef,
           ymin = lower,
           ymax = upper,
           fill = response,
           col = response))  
  geom_pointrange(position = position_dodge(width = 0.4), size = 2)  
  geom_hline(yintercept = 0)  
  scale_fill_manual(values = response.manual.col.scale)  
  scale_color_manual(values = response.manual.col.scale)  
  theme_bw()

So far I've managed to get a plot like this: enter image description here

I would like the plot to look like this: enter image description here

These are a few unsuccessful attempts at getting the plot I want.

ggplot(data = df,
       aes(x = explanatory,
           y = coef,
           ymin = lower,
           ymax = upper,
           fill = if_else(coef.zero == TRUE, true = "transparent", false = response),
           col = response))  
  geom_pointrange(position = position_dodge(width = 0.4), size = 2)  
  geom_hline(yintercept = 0)  
  # scale_fill_manual(values = response.manual.col.scale)  
  scale_color_manual(values = response.manual.col.scale)  
  theme_bw()


ggplot(data = df,
       aes(x = explanatory,
           y = coef,
           ymin = lower,
           ymax = upper,
           fill = resp.fill,
           col = resp.col))  
  geom_pointrange(position = position_dodge(width = 0.4), size = 2)  
  geom_hline(yintercept = 0)  
  theme_bw()


ggplot(data = df,
       aes(x = explanatory,
           y = coef,
           ymin = lower,
           ymax = upper,
           fill = response,
           col = response,
           # transparency based on condition
           alpha = if_else(coef.zero == TRUE, true = 0, false = 1)))  
  geom_pointrange(position = position_dodge(width = 0.4), size = 2)  
  geom_hline(yintercept = 0)  
  scale_fill_manual(values = response.manual.col.scale)  
  scale_color_manual(values = response.manual.col.scale)  
  theme_bw()  
  guides(alpha = "none")

Many thanks in advance!

CodePudding user response:

If you are going to the effort of creating colors in the data frame, you can use them with identity scales:

df <- data.frame(response = c("r1", "r1", "r2", "r2"),
                 explanatory = c("var1", "var2", "var1", "var2"),
                 coef = c(1, 2, 1, 2),
                 lower = c(-1, 1, 0.5, -2),
                 upper = c(4, 3, 1.5, 6)) %>% 
  mutate(
    # variable storing whether the confidence interval overlaps with 0
    coef.zero = lower/upper < 0,
    # variable storing the colour to be used for fill and colour
    resp.col = rep(c("#A6761D", "#FC8D62"), each = 2),
    resp.fill = if_else(coef.zero == TRUE, true = "white", false = resp.col))

ggplot(data = df,
       aes(x = explanatory,
           y = coef,
           ymin = lower,
           ymax = upper,
           fill = resp.fill,
           col = resp.col))  
  geom_pointrange(position = position_dodge(width = 0.4), size = 2,
                  shape = 21)  
  geom_hline(yintercept = 0)  
  scale_fill_identity()  
  scale_color_identity()  
  theme_bw()

enter image description here

  • Related