Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 箱线图两个框的一个x轴标记标签_R_Ggplot2 - Fatal编程技术网

R 箱线图两个框的一个x轴标记标签

R 箱线图两个框的一个x轴标记标签,r,ggplot2,R,Ggplot2,我试图在R中使用ggplot2创建一个箱线图,下面是我的代码和它生成的图。我想改变它,所以我不想让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.5

我试图在R中使用ggplot2创建一个箱线图,下面是我的代码和它生成的图。我想改变它,所以我不想让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"))
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"))