我在 R 上的 cowplot 包中使用 axis_canvas 时遇到问题:“zero_range(range) 错误:x 必须是长度 1 或 2”

问题描述 投票:0回答:0

我正在尝试在我使用 ggplot 创建的散点图旁边添加边际箱线图。为了添加边际箱线图,我使用了 cowplot 包(也是因为 ggmarginal 总是给我不同宽度的箱线图)。尽管如此,它仅适用于 x 轴而不适用于 y 轴。

这是我的散点图代码:

data_merged %>%  
  filter(treatment !="ctr") %>% 
  split(.$compartment)  %>% 
  map(
    ~ggplot(., 
            aes(x=time_after_treatment, y=max_peak, 
                fill=treatment, colour=treatment))+
      scale_colour_manual(values=c("#1f78b4", "#b2df8a", "#a6cee3")) +
      scale_fill_manual(values=c("#1f78b4", "#b2df8a", "#a6cee3")) +
      geom_point(size=3) +
      scale_x_continuous(limits= c(2.5,17))+
      scale_y_continuous (limits= c(0.4, 2.0, 0.5)) +
      theme_classic() +
      labs(y= Ca^"2+"~peak~(mu*M), x= "Time (min)") +
      theme(#axis.title.x=element_blank(), 
            legend.position = "none",
            axis.text=element_text(size=11),
            panel.border = element_blank(),
            panel.grid.minor = element_blank(),
            strip.background = element_blank()) 
  ) -> list_scatter

这是我用来创建边际 x 轴箱线图的代码,它工作正常

data_merged %>%  
  filter(treatment !="ctr") %>% 
  filter(compartment == "cytosol") -> data_cyt 

xbox_P3 <- axis_canvas(list_scatter[[1]], axis = "x", coord_flip = TRUE) +
  geom_boxplot(data=data_cyt, 
               aes(y=time_after_treatment, x=treatment, fill=treatment))+
  scale_fill_manual(values=c("#1f78b4", "#b2df8a", "#a6cee3"))+
  scale_x_discrete() +coord_flip()

P3 <- insert_xaxis_grob(list_scatter[[1]], xbox_P3, 
                        grid::unit(0.9, "in"), position="top")
P3_plot <- ggdraw(P3)

那是我用来尝试添加它不起作用的边际 y 轴箱线图的代码

ybox_P4 <- axis_canvas(list_scatter[[2]], axis = "y") +
  geom_boxplot(data=data_cyt, 
               aes(y=max_peak, x=treatment, fill=treatment))+
  scale_fill_manual(values=c("#1f78b4", "#b2df8a", "#a6cee3"))+
  scale_x_discrete()

但是它给了我这个错误信息

zero_range(range) 错误:x 必须是长度 1 或 2

  1. stop("x 的长度必须是 1 或 2")
  2. zero_range(范围)
  3. scales::expand_range(range, expand[1], expand[2])
  4. get_scale_limits(layer_scales(plot)$y)
  5. axis_canvas(list_scatter[[2]], axis = "y")
r ggplot2 cowplot
© www.soinside.com 2019 - 2024. All rights reserved.