Home > other >  Independently changing the plot element sizes when using stat_pointinterval()
Independently changing the plot element sizes when using stat_pointinterval()

Time:05-09

I've been trying to use the stat_pointinterval() function from the tidybayes packages to create some figures but am running into some issues customizing the look. I've added an example of the code I'm using below as well as an example of what the output figure looks like. What I'm trying to do is find a way to change the size of the dot and each of the intervals independently. So far I've been able to change the size of the dot but I've only been able to find a way to change the width of the entire line, not each segment independently (see second figure below). Any help would be great, or if anyone has any better ideas of how to approach creating plots like this. Thanks!

library(tidyverse)
library(tidybayes)

treat <- rep(c("Control", "T1", "T2", "T4"),100)
est <- rnorm(400,0,1)

df <- as.data.frame(cbind(treat, est)) %>% 
        mutate(treat = as.factor(treat),
               est = as.numeric(est))


ggplot(data = df, aes(x=treat, y=est))  
  stat_pointinterval()

enter image description here

ggplot(data = df, aes(x=treat, y=est))  
  stat_pointinterval(.width = c(0.5,0.95),
                     size = 10)

enter image description here

CodePudding user response:

One approach to set the size of the point and segments independently would be to set the dot size via the point_size argument and set the segment sizes via the range argument of scale_size_continuous where the default is range=c(1, 6):

library(tidyverse)
library(tidybayes)

set.seed(123)

treat <- rep(c("Control", "T1", "T2", "T4"), 100)
est <- rnorm(400, 0, 1)

df <- as.data.frame(cbind(treat, est)) %>%
  mutate(
    treat = as.factor(treat),
    est = as.numeric(est)
  )

ggplot(data = df, aes(x = treat, y = est))  
  stat_pointinterval(
    .width = c(0.5, 0.95),
    point_size = 10
  )  
  scale_size_continuous(range = c(5, 20))

A second option would be to use after_stat to map factor(.width) (where factor converts to a discrete variable) on the size aes and set the sizes via scale_size_manual:

ggplot(data = df, aes(x = treat, y = est))  
  stat_pointinterval(
    aes(size = after_stat(factor(.width))),
    .width = c(0.5, 0.95),
    point_size = 10
  )  
  scale_size_manual(values = c("0.5" = 10, "0.95" = 5))

  • Related