Home > Net >  ggplot boxplot not plotting when supplying breaks
ggplot boxplot not plotting when supplying breaks

Time:03-12

I have 7 values that I am trying to plot using geom_boxpot and I wish to only show certain values in the legend so am using the breaks argument in the scale_linetype_manual function but it doesn't plot those boxplots at all,

Boxplot using scale_linetype_manual

However, if I use the same code with scale_linetype_discrete it works fine and plots and only gives me the required values in the legend. But, I can't control the linetype in the function using the values argument. Is there a way to add values to the scale_linetype_discrete function?

Boxplot using scale_linetype_discrete

EDIT - UPDATED with fake data code

> head(debug)
# A tibble: 6 × 4
   subj time  cond        y
  <dbl> <chr> <chr>   <dbl>
1     1 one   one_A       2
2     1 two   two_A       1
3     1 two   two_B       5
4     1 two   two_C       0
5     1 three three_A     4
6     1 four  four_A      4

> dput(debug)
structure(list(subj = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4), time = c("one", 
"two", "two", "two", "three", "four", "four", "one", "two", "two", 
"two", "three", "four", "four", "one", "two", "two", "two", "three", 
"four", "four", "one", "two", "two", "two", "three", "four", 
"four"), cond = c("one_A", "two_A", "two_B", "two_C", "three_A", 
"four_A", "four_B", "one_A", "two_A", "two_B", "two_C", "three_A", 
"four_A", "four_B", "one_A", "two_A", "two_B", "two_C", "three_A", 
"four_A", "four_B", "one_A", "two_A", "two_B", "two_C", "three_A", 
"four_A", "four_B"), y = c(2, 1, 5, 0, 4, 4, 2, 2, 4, 3, 0, 1, 
5, 3, 1, 5, 4, 2, 0, 4, 4, 0, 0, 4, 2, 1, 5, 5)), row.names = c(NA, 
-28L), spec = structure(list(cols = list(subj = structure(list(), class = c("collector_double", 
"collector")), time = structure(list(), class = c("collector_character", 
"collector")), cond = structure(list(), class = c("collector_character", 
"collector")), y = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x600004737380>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

# Scale_linetype_discrete (would like to add linetype values to this)

ggplot()  
  geom_boxplot(debug,
               mapping = aes(x = time, 
                             y = y, 
                             linetype = cond),
               show.legend = TRUE)  
  scale_x_discrete(limits = c("one","two","three","four"),
                   labels = c("one","two","three","four"))  
  scale_linetype_discrete(name = "My legend",
                          breaks = c("two_A","two_B","two_C","four_A","four_B"),
                          labels = c("two_A","two_B","two_C","four_A","four_B")) 

# Scale_linetype_manual (would like 'one' and 'three' to be actually plotted but not showing in my legend)

ggplot()  
  geom_boxplot(debug,
               mapping = aes(x = time, 
                             y = y, 
                             linetype = cond),
               show.legend = TRUE)  
  scale_x_discrete(limits = c("one","two","three","four"),
                   labels = c("one","two","three","four"))  
  scale_linetype_manual(name = "My legend",
                        breaks = c("two_A","two_B","two_C","four_A","four_B"),
                        labels = c("two_A","two_B","two_C","four_A","four_B"),
                        values = c("solid","dashed","solid","dashed","solid", "dashed","solid")) 

CodePudding user response:

Not sure about the reasons why your code does not work. But one option to fix your issue would be to make use of a named vector to assign linetypes to categories of you cond variable:

library(ggplot2)

lty <- c("solid", "dashed", "solid", "dashed", "solid", "dashed", "solid")
names(lty) <- c("four_A", "four_B", "one_A", "three_A", "two_A", "two_B", "two_C")

ggplot()  
  geom_boxplot(debug,
    mapping = aes(
      x = time,
      y = y,
      linetype = cond
    )
  )  
  scale_x_discrete(
    limits = c("one", "two", "three", "four"),
    labels = c("one", "two", "three", "four")
  )  
  scale_linetype_manual(
    name = "My legend",
    breaks = c("two_A", "two_B", "two_C", "four_A", "four_B"),
    values = lty
  )

  • Related