Alex
Alex

Reputation: 63

R ggplot: ggpattern and swimmer plot

Can I add a pattern (from ggpattern package) to a swimmer bar plot (from swimplot package)? The swimmer plot is form of ggplot.

Any help with fixing my code would be most helpful.

df <- data.frame(
  study_id = c(3, 3, 3), primary_therapy = c("Si", "Si", "Si"),
  additional_treatment = 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(pattern = additional_treatment, pattern_angle = additional_treatment))

When I tried this code it could not add the second layer and said

"Problem while computing aesthetics."

Upvotes: 0

Views: 238

Answers (1)

stefan
stefan

Reputation: 124013

Assuming that you want to map additional_treatment on the pattern aes you have to explicitly map on the x and y aes in geom_col_pattern to add a pattern to your swimmer plot:

library(swimplot)
library(ggpattern)
library(ggplot2)

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_treatment, pattern_angle = additional_treatment
  ))

enter image description here

Upvotes: 1

Related Questions