rggplot2plotstatisticstukey

In a ggplot2 plot, changing legend name results in showing names twice


I'm trying to plot interactions plot with Tukey confidence intervals. Plot the difference in length depending on dose and supp. These things already work in the example I show at the bottom of the question.

I would like to show different linetypes, different shapes and different colours but only to differentiate 2 options supp(OJ or VC). OJ dotted line, blue, with mean as a circle, VC normal line, red, with mean as a square.

My two problems are:

  1. if I set shapes and colors, then when changing legend name (scale_colour_discrete(name = "SUPPrename")) I end up getting two different legends.
  2. I could add different shapes as mean, and different line colours, but couldn't change linetype too.

enter image description here

library("ggpubr")
require("dplyr")
data("ToothGrowth")

# Ahora  tukey con anova two ways 
# Store the data in the variable df
df <- ToothGrowth

# Show a random sample
set.seed(1234)
dplyr::sample_n(df, 10)

# Convert dose as a factor and recode the levels        # as "D0.5", "D1", "D2"
df$dose <- factor(df$dose,
                  levels = c(0.5, 1, 2),
                  labels = c("D0.5", "D1", "D2"))




# MF anova      # Two-way ANOVA with interaction effect     # These two calls are equivalent
#res.aov3 <- aov(len ~ supp * dose, data = df)
res.aov3 <- aov(len ~ supp + dose + supp:dose, data = df)
summary(res.aov3)

# Use TukeyHSD
tukeyHSD <- TukeyHSD(res.aov3, which = "dose")
plot(tukeyHSD)

# Allans response 
tukeyCI <- (tukeyHSD$dose[1, 1] - tukeyHSD$dose[1, 2]) / 2


print("Example of an error that happens when I set different point shapes and change the leyends name.")
ggplot(df, aes(x = dose, y = len, color = supp, group = supp)) + 
    stat_summary(fun.y = mean, fun.ymin = function(x) mean(x) - tukeyCI, fun.ymax = function(x) mean(x) + tukeyCI, geom = "errorbar", size = 1, color = 'gray50', width = 0.25) +
               scale_shape_manual(values = 21:26) + 
               stat_summary(fun.y = function(x) mean(x), geom = "line", aes(group = supp)) +
               stat_summary(fun.y = mean, geom = "point", size = 3, aes(shape = supp), col = "black", fill = 'white') + 
               theme_minimal(base_size = 16) +
               theme(panel.background = element_rect(fill = "white", colour = "black")) +
               scale_colour_discrete(name = "NewName ERROR")

Solution

  • To get your legends merged you have to use the same name for both legends which IMHO is best done via labs. Not sure what's the issue with the linetype but you add it to the stat_summary adding the lines and of course in that case set the name for the linetype legend too.

    ggplot(df, aes(x = dose, y = len, color = supp, group = supp)) + 
      stat_summary(fun.y = mean, fun.ymin = function(x) mean(x) - tukeyCI, fun.ymax = function(x) mean(x) + tukeyCI, geom = "errorbar", size = 1, color = 'gray50', width = 0.25) +
      scale_shape_manual(values = 21:26) + 
      stat_summary(fun.y = function(x) mean(x), geom = "line", aes(group = supp, linetype = supp)) +
      stat_summary(fun.y = mean, geom = "point", size = 3, aes(shape = supp), col = "black", fill = 'white') + 
      theme_minimal(base_size = 16) +
      theme(panel.background = element_rect(fill = "white", colour = "black")) +
      labs(color = "NewName ERROR", shape = "NewName ERROR", linetype = "NewName ERROR")
    

    enter image description here