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

I have this code and all the boxed are green, what can i add to this to make the boxes different colors

rawr
  • 20,481
  • 4
  • 44
  • 78
  • 1
    you can pass a vector of values in `col` Here, there are 3 boxplots, so the number of colours can be 3. `boxplot(mtcars$mpg ~ mtcars$cyl, main = "Box Plot of Mileage vs Number of Cylinders", xlab = "Number of Cylinders", ylab = "Miles per Gallon", col = c("lightgreen", "red", "blue"))` – akrun May 21 '20 at 03:17
  • Does this answer your question? [Colouring different group data in boxplot using r](https://stackoverflow.com/questions/15212884/colouring-different-group-data-in-boxplot-using-r) – UseR10085 May 21 '20 at 04:34

1 Answers1

0

You just need to add a factor(x_axis_var) for color.

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))

or provide a vector of colors:

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"))
Shan R
  • 521
  • 4
  • 8