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