在R中为Boxplot添加颜色

时间:2020-05-21 03:15:37

标签: r

ClientScript.RegisterClientScriptBlock

我有此代码,所有方框均为绿色,我可以添加些什么以使方框具有不同的颜色

1 个答案:

答案 0 :(得分:0)

您只需要为颜色添加一个因数(x_axis_var)。

boxplot(mtcars$mpg ~ mtcars$cyl, 
        main = "Box Plot of Mileage vs Number of Cylinders", 
        xlab = "Number of Cylinders", 
        ylab = "Miles per Gallon", col = factor(mtcars$cyl))

或提供颜色矢量:

boxplot(mtcars$mpg ~ mtcars$cyl, 
        main = "Box Plot of Mileage vs Number of Cylinders", 
        xlab = "Number of Cylinders", 
        ylab = "Miles per Gallon", col = c("blue","red","green"))