5

I'm using the following data frame:

df1 <- structure(list(Genotype = structure(c(1L, 1L, 1L, 1L, 1L,
2L,2L,2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L,1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L),
.Label= c("miR-15/16 FL", "miR-15/16 cKO"), class = "factor"), 
Tissue = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L), .Label = c("iLN", "Spleen", "Skin", "Colon"), class = "factor"), 
`Cells/SC/Live/CD8—,, CD4+/Foxp3+,Median,<BV421-A>,CD127` = c(518L, 
715L, 572L, 599L, 614L, 881L, 743L, 722L, 779L, 843L, 494L, 
610L, 613L, 624L, 631L, 925L, 880L, 932L, 876L, 926L, 1786L, 
2079L, 2199L, 2345L, 2360L, 2408L, 2509L, 3129L, 3263L, 3714L, 
917L, NA, 1066L, 1059L, 939L, 1269L, 1047L, 974L, 1048L, 
1084L)),
.Names = c("Genotype", "Tissue", "Cells/SC/Live/CD8—,,CD4+/Foxp3+,Median,<BV421-A>,CD127"),
row.names = c(NA, -40L), class = c("tbl_df", "tbl", "data.frame"))

and trying to make a plot using ggplot2 where box plots and points are displayed grouped by "Tissue" and interleaved by "Genotype". This seems to be working well, however, I'm noticing that in my legend, there is an additional "a" that is being overlaid with the shape.

library(ggplot2)
library(ggpubr)
color.groups <- c("black","red")
names(color.groups) <- unique(df1$Genotype)
shape.groups <- c(16, 1)
names(shape.groups) <- unique(df1$Genotype)

ggplot(df1, aes(x = Tissue, y = df1[3], color = Genotype, shape = Genotype)) +
  geom_boxplot(position = position_dodge(), outlier.shape = NA) +
  geom_point(position=position_dodge(width=0.75)) +
  ylim(0,1.2*max(df1[3], na.rm = TRUE)) +
  ylab('MFI CD127 (of CD4+ Foxp3+ T cells') +
  scale_color_manual(values=color.groups) +
  scale_shape_manual(values=shape.groups) +
  theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
                     axis.title.x=element_blank(), aspect.ratio = 1,
                     text = element_text(size = 9)) +
  stat_compare_means(label = 'p.format', method = 't.test',
                     label.y = c(0.1*max(df1[3], na.rm = TRUE) + max(df1[3][c(1:10),], na.rm = TRUE),
                                 0.1*max(df1[3], na.rm = TRUE) + max(df1[3][c(11:20),], na.rm = TRUE),
                                 0.1*max(df1[3], na.rm = TRUE) + max(df1[3][c(21:30),], na.rm = TRUE),
                                 0.1*max(df1[3], na.rm = TRUE) + max(df1[3][c(31:40),], na.rm = TRUE)))

enter image description here

If I hide the scale_color_manual legend, it removes the "a"s but it also removes the color from the legend. Am I missing something?

Thanks for any help!

5
  • 4
    Try to add show.legend = FALSE in stat_compare_means. See also Remove 'a' from legend when using aesthetics and geom_text
    – Henrik
    Commented Sep 27, 2017 at 20:34
  • I ran your code with and without scale_color_manual and the as are present on both runs. Commented Sep 27, 2017 at 20:34
  • @Henrik It worked! Care to explain why? Commented Sep 27, 2017 at 20:36
  • 2
    stat_compare_means inherits the mapping from the top level in ggplot, not only x and y, but also color = Genotype. Although the text from stat_compare_means is not colored by Genotype in the plot, the default geom_text symbol for legend (the "a") is still added to the legend. Seems unnecessary to me.
    – Henrik
    Commented Sep 27, 2017 at 20:43
  • Seems like strange behavior but thank you, it worked perfectly! Commented Sep 27, 2017 at 20:57

0

Browse other questions tagged or ask your own question.