您的问题有两个部分:1)控制图例标签和2)固定x轴geom_col_pattern()
.
对于第一个问题,您可以删除"NA"
从传说中通过转换additional_therapy
到一个因素:
df <- df %>%
mutate(additional_therapy = factor(additional_therapy,
levels = c("S", "V", "V+S", "NA")))
然后,您可以指定breaks
控制显示哪些图例标签:
+ scale_pattern_manual(name="Additional Therapy", breaks = c("S", "V", "V+S"), values = c("S"="stripe", "V"="circle", "V+S"="crosshatch", "NA"="none"))
然而,据我所知,存在一个问题ggpattern
当涉及到重复模式时。我能够使用 base 重新创建这个问题ggplot2
确认这不是问题swimplot
.
在下面的代码中,您可以看到fill
行为适当,但是pattern
永远不会重复两次,这会导致模式"S"
溢出到第二个红色条,它不应该有任何图案,因为它是"NA"
。我建议在 Github 上提交问题 https://github.com/coolbutuseless/ggpattern/issues.
library(ggpattern)
library(tidyverse)
df <- data.frame(
study_id = c(3, 3, 3, 3), primary_therapy = c("Si", "Si", "Si", "Si"),
additional_therapy = c("NA", "S", "NA", "V+S"), end_yr = c(0.08, 0.39, 3.03, 3.4))
df %>%
ggplot(aes(x = end_yr, y = study_id)) +
geom_col_pattern(aes(pattern = additional_therapy,
fill = additional_therapy),
color = NA,
show.legend = TRUE,
position = "fill",
width = 3,
pattern_spacing = 0.01,
pattern_fill = "black",
pattern_color = NA,
pattern_size = 0.5,
pattern_density = 0.1,
pattern_linetype = 0.5,
pattern_orientation = "vertical") +
scale_pattern_manual(
name = "Additional Therapy",
breaks = c("S", "V", "V+S"),
values = c("S" = "stripe", "V" = "circle", "V+S" = "crosshatch", "NA" = "none"))