2016-09-22 20 views
0

Rでggplot2を使ってboxplotを作成しようとしていますが、これは私のコードとそれが生成するプロットです。 0.5mg、0.5mg、1mg、1mg、2mg、2mgというラベルが付けられているのではなく、0.5mg、1mg、2mgの2つのボックスプロットのセットの間に変更したいと思います。これを行う方法はありますか?Boxplot 2つのボックスの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

関連する問題