ggplot2使用theme_classic主题调用geom_boxplot时无法移除x轴刻度和文本
报错原因
- ggplot2的预设主题函数(包括
theme_classic()、theme_bw()等)仅支持接收基础字号、字体、边距这类全局主题参数,不支持直接传入axis.text.x、axis.ticks.x这类单元素修改参数,这是触发「unused arguments」报错的根本原因。 - 要在预设主题基础上修改局部元素,需要额外叠加
theme()函数传入自定义参数。
修正后可运行代码
p2 <- ggplot(data = sal_pH, aes(x = NULL, y = quant)) + stat_boxplot(geom = "errorbar", width = 0.3) + geom_boxplot( color='#00008B', fill='#00008B', alpha=0.4, outlier.color='blue1', outlier.fill='white', outlier.size=2 ) + coord_flip() + theme_classic() + # 先加载经典主题预设 theme( # 单独传入自定义主题修改项 axis.text.x = element_blank(), axis.ticks.x = element_blank() ) + xlab('Salem') + ylab('pH') + ylim(6.0, 10.0)
补充说明:由于你调用了
coord_flip()翻转了坐标轴,如果你实际想要移除的是翻转后位于水平方向的原Y轴刻度,将上述theme()中的axis.text.x、axis.ticks.x替换为axis.text.y、axis.ticks.y即可。
内容的提问来源于stack exchange,提问作者Rich Shepard
相关产品推荐
相关产品推荐

