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