Boxplot一个x轴刻度标记标签为两个框

时间:2016-09-22 21:30:48

标签: r ggplot2

我试图在R中使用ggplot2创建一个boxplot,下面是我的代码和它生成的图。我想改变它而不是将x轴标记为0.5mg,0.5mg,1mg,1mg,2mg和2mg,我想在每组两个箱形图之间仅0.5mg,1mg和2mg。有没有办法做到这一点?

boxplot

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"))

1 个答案:

答案 0 :(得分:1)

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"))

结果: enter image description here