R语言cowplot包axis_canvas报错:x must be length 1 or 2
问题:cowplot添加y轴边际箱线图触发
zero_range错误 我用ggplot绘制散点图并尝试添加边际箱线图,因ggmarginal生成的箱线图宽度不一致,选择使用cowplot包。x轴边际箱线图可正常生成,但添加y轴边际箱线图时触发错误:Error in zero_range(range) : x must be length 1 or 2。
散点图代码
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()
错误信息
Error in zero_range(range) : x must be length 1 or 2
5. stop("x must be length 1 or 2")
4. zero_range(range)
3. scales::expand_range(range, expand[1], expand[2])
2. get_scale_limits(layer_scales(plot)$y)
- axis_canvas(list_scatter[[2]], axis = "y")
错误原因
问题出在散点图代码的scale_y_continuous参数上:limits= c(0.4, 2.0, 0.5)。limits参数只需要传入长度为2的向量(最小值和最大值),但额外添加的第三个值0.5导致y轴范围解析异常。当axis_canvas尝试读取y轴范围时,调用的zero_range函数要求输入的范围向量长度必须是1或2,因此触发错误。
解决方法
将scale_y_continuous的limits参数修正为仅包含最小值和最大值,若需要设置刻度间隔,改用breaks参数:
修正后的散点图代码片段
scale_y_continuous(limits= c(0.4, 2.0), breaks = seq(0.4, 2.0, 0.5)) +
完整修正后的散点图代码
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), breaks = seq(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
修正后重新运行y轴箱线图代码
此时再执行报错的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() # 插入到散点图中 P4 <- insert_yaxis_grob(list_scatter[[2]], ybox_P4, grid::unit(0.9, "in"), position="right") P4_plot <- ggdraw(P4)
内容的提问来源于stack exchange,提问作者Marco Giovannetti
相关产品推荐
相关产品推荐

