Search code examples
rggplot2figure

Different group sizes for geom_jitter() and stat_summery()


This is my first post. Please let me know if you need additional information to answer this question!

I have been trying to find out how to set different sizes for groups in geom_jitter() and stat_summery().

figure made with the R script below

In this figure, 4 shapes are set for the 4 groups and I want to use the same shapes for the individual plot (using geom_jitter()) and the mean values of each group (using stat_summery()). I specify the sizes of the each shape as well because these shapes look slightly different in size if I use the same size for all of them. It turns out that geom_jitter() and stat_summery() use different definitions of size, so I need to use different values for "size" in geom_jitter() and stat_summery(). However, I cannot use scale_size_manual() for both of them, so I need to use a common size for either geom_jitter() or stat_summery(). This figure uses a common size for the mean values, but you see that the diamond for Group=1000 look smaller than the other three means. Here is my R script for this figure.

library(ggplot2)  
library(tidyverse)
x %>%
  ggplot(aes(x=Time,y=value),group=Group)+
  geom_jitter(aes(color = Group,shape = Group,size = Group), 
              position = position_jitterdodge(jitter.width=0.2,dodge.width=0.6),alpha=0.3 ) +
  scale_shape_manual(values =  c(15,16,17,18))+
  scale_size_manual(values =  c(6,7,5,7))+
  scale_color_manual(values =  c("green","orange", "red", "blue"))+
  stat_summary(aes(group = Group,shape= Group),
               size=1.3, 
               fun.data="mean_cl_normal",  
               geom = "pointrange",  position = position_dodge(0.6)  )+
  scale_x_discrete(limits=c("90","150"))+ 
  coord_cartesian(ylim=c(0,10))+
  theme_classic()+
  labs(y="lab value",x="lab time", fill="lab group")+
  theme(axis.text.x=element_text(size=12,colour = "black"),axis.text.y=element_text(size=12,colour = "black"),axis.title=element_text(size=14,colour = "black"))

I will be very grateful if anyone could tell me how to set different sizes for those 4 shapes of the mean values. I would also appreciate if anyone could tell me how to change the title of the legend (labs(fill="") doesn't work for some reason).


Solution

  • As already mentioned in the comments you are using the color aes, hence you have to set the legend title via color= not fill=. Additionally you have to use the same title for all legends, i.e. do labs(..., color="lab group", shape ="lab group", size="lab group"). Otherwise the legends will not get merged. Second, to get the same sizes for the geom_pointrange map on the size aes too and set fatten=1. Finally I have set show.legend=FALSE to not display the point range in the legend.

    Using some fake random example data:

    library(ggplot2)
    
    set.seed(123)
    
    x <- data.frame(
      Time = factor(c(90, 150)),
      value = runif(100, 0, 10),
      Group = sample(LETTERS[1:4], 100, replace = TRUE)
    )
    
    ggplot(x, aes(x = Time, y = value)) +
      geom_jitter(aes(color = Group, shape = Group, size = Group),
        position = position_jitterdodge(jitter.width = 0.2, dodge.width = 0.6), 
        alpha = 0.3
      ) +
      scale_shape_manual(values = c(15, 16, 17, 18)) +
      scale_size_manual(values = c(6, 7, 5, 7)) +
      scale_color_manual(values = c("green", "orange", "red", "blue")) +
      stat_summary(aes(group = Group, shape = Group, size = Group),
        fun.data = "mean_cl_normal",
        fatten = 1,
        geom = "pointrange", position = position_dodge(0.6),
        show.legend = FALSE
      ) +
      scale_x_discrete(limits = c("90", "150")) +
      coord_cartesian(ylim = c(0, 10)) +
      theme_classic() +
      labs(y = "lab value", x = "lab time", 
           color = "lab group", shape = "lab group", size = "lab group") +
      theme(
        axis.text.x = element_text(size = 12, colour = "black"),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.title = element_text(size = 14, colour = "black")
      )