Boxplot两个框的一个x轴刻度标记标签 [英] Boxplot one x axis tick mark label for two boxes

查看:156
本文介绍了Boxplot两个框的一个x轴刻度标记标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中使用ggplot2创建箱形图,以下是我的代码及其生成的图.我想要更改它,而不是将x轴分别标记为0.5mg,0.5mg,1mg,1mg,2mg和2mg,而我只希望在两个箱形图的每组之间分别设置0.5mg,1mg和2mg.有什么办法吗?

I'm trying to create a boxplot using ggplot2 in R, below is my code and the plot it produces. I want to change it so instead of having the x axis labelled as 0.5mg, 0.5mg, 1mg, 1mg, 2mg and 2mg, I want just 0.5mg, 1mg, and 2mg between each of the sets of two box plots. Is there any way to do this?

ggplot(ToothGrowth, aes(x=interaction(supp, dose), y=len, fill=supp)) + 
geom_boxplot() +
scale_x_discrete(labels = c("0.5mg", "0.5mg", "1mg", "1mg", "2mg", "2mg"), name = "Dosage") +
scale_y_continuous(name = "Tooth Length") + 
scale_fill_discrete(name = "Supplement",
                    labels = c("Orange Juice", "Ascorbic Acid"))

推荐答案

library(ggplot2)
ggplot(ToothGrowth, aes(x= as.factor(dose), y=len, fill=supp)) + 
  geom_boxplot() +
  scale_x_discrete(name = "Dosage", labels = function(x) {paste0(x, "mg")}) + 
  scale_y_continuous(name = "Tooth Length") + 
  scale_fill_discrete(name = "Supplement",
                    labels = c("Orange Juice", "Ascorbic Acid"))

结果:

这篇关于Boxplot两个框的一个x轴刻度标记标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆