2016-09-22 97 views
0

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

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