2016-09-22 129 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