0

I am using ggarrange to produce a figure with three plots. One of these plots includes fill and a pattern created using ggpattern. I am able to create a combined figure when I combine one standard ggplot and the ggpattern plot, but I get an error (Error in seq.default(from, to, by) : invalid '(to - from)/by') when I try to combine all 3. I've included a simplified example below.

#fake data
data <- structure(list(Level= c(0.2, 0.3, 0.25, 0.35, 0.4, 0.5, 0.5, 0.6, 0.15, 0.35), 
                       Group= c("A", "A", "B", "B", "C", "C", "D", "D", "E", "E"), 
                       Condition = c("no", "yes", "no", "yes", "no", "yes", "no", "yes", "no", "yes"),
                       Hx = c(0,1,1,1,0,1,0,0,0,0),
                       Type = c("T", "T", "T", "T", "T", "F", "F", "F", "F", "F")), 
                  row.names = c(NA, -10L), 
                  class = c("tbl_df", "tbl", "data.frame"))

#create plots 

pattern_plot <- 
  ggplot(data = data, aes(x = Group, y = Level)) +
  facet_grid(cols=vars(Type)) +
  geom_bar_pattern(aes(pattern = Condition, fill = as.factor(Hx)), 
                   stat = "identity",
                   position = "dodge",
                   color = "black", 
                   pattern_fill = "black",
                   pattern_angle = 45,
                   pattern_density = 0.1,
                   pattern_spacing = 0.025,
                   pattern_key_scale_factor = 0.6) + 
  scale_pattern_manual(values = c("none", "stripe")) + 
  labs(x = "", y = "", pattern = "Condition", fill = "History", 
       subtitle = "Percentage of people with condition") +
  guides(pattern = guide_legend(override.aes = list(fill = "white")),
         fill = guide_legend(override.aes = list(pattern = "none"))) +
  theme_bw() +
  theme(legend.position="left") +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1))

no_pattern_plot <- 
  ggplot(data = data, aes(x = Group, y = Level, fill = Condition)) + 
  geom_bar(position = "dodge", stat = "identity") + 
  theme_bw() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  theme(legend.position="left")

no_pattern_plot2 <-   ggplot(data = data, aes(x = Group, y = Level, fill = as.factor(Hx))) + 
  geom_bar(position = "dodge", stat = "identity") + 
  theme_bw() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  theme(legend.position="left")

This works:

ggarrange(pattern_plot, no_pattern_plot2, nrow = 2)

To create this plot

However ggarrange(pattern_plot, no_pattern_plot, no_pattern_plot2, nrow = 3) produces Error in seq.default(from, to, by) : invalid '(to - from)/by'

Any ideas?

ekazura
  • 1
  • 1

0 Answers0