Home > Mobile >  ggpattern customize legend / manual scale pattern
ggpattern customize legend / manual scale pattern

Time:01-27

I want to customize my legend and the pattern shapes in ggpattern but when I run the code two legends appear on the side. I also would like the pattern color/stripes to be black when right now they are grey even when pattern_color="black.

df <- data.frame(
      study_id = c(3, 3, 3), primary_therapy = c("Si", "Si", "Si"),
      additional_therapy = c("NA", "S", "V S"), end_yr = c(0.08, 0.39, 3.03)
    )

swimmer_plot(
  df = df, id = "study_id",
  end = "end_yr", name_fill = "primary_therapy",
  width = 0.85, color = NA
)   geom_col_pattern(aes(study_id, end_yr,
    pattern = additional_therapy, pattern_angle = additional_therapy), 
    fill = NA, na.rm=FALSE, show.legend=NA, width=0.85,
    pattern_spacing = 0.01, pattern_color = "black", pattern_size = 0.5, 
    pattern_linetype = 0.5, pattern_orientation="vertical")  
  scale_pattern_manual(name="Additional Therapy", values = c("S"="stripe","NA"="none","V S"="circle"))

CodePudding user response:

First, with geom_col_pattern, show.legend = can only be TRUE or FALSE; NA isn't a valid option. Second, remove the pattern_angle argument geom_col_pattern, since I think that's what's causing the second legend. Third, change pattern_color to pattern_fill to make the lines black.

library(swimplot)
library(ggpattern)
library(tidyverse)

df <- data.frame(
  study_id = c(3, 3, 3), primary_therapy = c("Si", "Si", "Si"),
  additional_therapy = c("NA", "S", "V S"), end_yr = c(0.08, 0.39, 3.03)
)

swimmer_plot(
  df = df, id = "study_id",
  end = "end_yr", name_fill = "primary_therapy",
  width = 0.85, color = NA)   
  geom_col_pattern(aes(x = study_id, y = end_yr,
                       pattern = additional_therapy), 
                     show.legend = TRUE,
                     fill = NA, 
                     na.rm = FALSE, 
                     width = 0.85, 
                     pattern_spacing = 0.01, 
                     pattern_fill = "black", 
                     pattern_size = 0.5, 
                     pattern_linetype = 0.5, 
                     pattern_orientation = "vertical")  
  scale_pattern_manual(name="Additional Therapy", 
                       values = c("S"="stripe","NA"="none","V S"="circle")) 

enter image description here

  • Related