R ggpattern custom legend

Viewed 1035

I'm not able to customise the legends I have with ggplot/ggpattern.

  1. The "SPEED" legend should not display any pattern (just the 3 colors)
  2. The "TYPE" legend should have custom texts instead of 0 and 1

thanks!

code:

remotes::install_github("coolbutuseless/ggpattern")
library(ggplot2)
library(ggpattern)

list <- c("1" = "circle", "0" = "none")
ggplot(tt) +
  aes(x = SOUND, y = R, fill = SPEED, pattern = TYPE) +
  geom_boxplot_pattern(pattern_fill = "white",
                       pattern_density = 0.6,
                       pattern_spacing = 0.012,
                       outlier.size = 1) +
  scale_pattern_manual(values = list)  +
  scale_fill_brewer(palette = "Set1") +
  labs(y = "R", title = "R") +
  ylim(0,100) +
  theme_minimal()

data sample:

structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 34L, 34L, 34L, 34L, 34L, 
34L, 34L, 34L, 34L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 
35L, 35L, 35L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
36L, 36L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 38L, 38L, 38L, 38L, 38L, 38L), .Label = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", 
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", 
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47"), class = "factor"), 
    TYPE = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), .Label = c("0", "1"), class = "factor"), SPEED = structure(c(1L, 
    1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 
    2L, 2L, 3L, 3L, 3L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 
    3L, 3L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 
    2L, 3L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 3L, 1L, 1L, 
    1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 
    2L, 3L, 3L, 3L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 
    3L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("1", 
    "2", "3"), class = "factor"), SOUND = structure(c(1L, 2L, 
    3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 4L, 4L, 1L, 2L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L, 4L, 4L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
    3L, 4L, 4L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 4L, 
    4L, 1L, 2L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 4L, 4L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 4L, 4L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 4L, 4L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    4L, 4L, 4L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("1", "2", 
    "3", "4"), class = "factor"), R = c(64, 77, 32, 75, 80, 38, 
    79, 57, 17, 54, 54, 50, 52, 58, 34, 30, 46, 0, 63, 51, 1, 
    18, 55, 18, 76, 77, 26, 60, 17, 0, 68, 62, 0, 1, 64, 43, 
    71, 37, 38, 53, 59, 20, 67, 55, 37, 43, 61, 58, 81, 73, 62, 
    54, 36, 51, 57, 27, 55, 49, 56, 75, 92, 62, 92, 92, 62, 87, 
    92, 62, 89, 92, 92, 92, 60, 36, 73, 14, 62, 76, 55, 45, 78, 
    68, 18, 92, 85, 38, 92, 86, 43, 87, 66, 40, 81, 71, 81, 36, 
    92, 22, 92, 82, 42)), row.names = c(NA, -101L), class = c("tbl_df", 
"tbl", "data.frame"))

enter image description here

the editor asks to add some more text since my post is mostly code but I don';t have much else to add. most code in fact it's just the sample data that I don't know how else to produce (I tried a couple of R default sets but they didn't work)

1 Answers

You can change legend key labels within the relevant scale_*_*() function by passing a vector with the labels to the labels argument.

To change the legend without changing the plot you most often need override.aes in guide_legend(). Here is an example using it within scale_pattern_manual() and overriding the pattern aesthetic by setting it to "none".

ggplot(tt) +
    aes(x = SOUND, y = R, fill = SPEED, pattern = TYPE) +
    geom_boxplot_pattern(pattern_fill = "white",
                         pattern_density = 0.6,
                         pattern_spacing = 0.012,
                         outlier.size = 1) +
    scale_pattern_manual(values = list, 
                         labels = c("Text1", "Text2"))  +
    scale_fill_brewer(palette = "Set1", 
                      guide = guide_legend(override.aes = list(pattern = "none"))) +
    labs(y = "R", title = "R") +
    ylim(0,100) +
    theme_minimal()

Created on 2021-07-02 by the reprex package (v2.0.0)

Related