Change the color of a subset of group labels in a

2019-08-15 14:07发布

问题:

How do I change the color of only a subset of labels in a simple boxplot? For example, on the X-axis I have groups "3", "4", and "5", and I only want to change the color of the "4" group label.

It's easy to change the color of the boxplot itself with the col="red", or even the border of the boxplot with border="red", but I cannot find any reference to changing JUST the label.

Example: boxplot(mtcars$hp~mtcars$gear)

Thanks!

回答1:

You could also try mtext:

b <- boxplot(count ~ spray, data = InsectSprays, col = "lightgray", axes = FALSE)
axis(2); axis(1, labels = NA); box()
mtext(b$names, at = 1:length(b$names), side = 1, line = 1, 
      col = ifelse(b$names == "B", "red", "black"))


回答2:

Try

 boxplot(hp~gear, mtcars)

 Colr <- c('black', 'red', 'black')
 for(i in seq_along(Colr)){
  axis(side=1, at=i, col.axis=Colr[i], 
        labels= sort(unique(mtcars$gear))[i] , las=1)

  }


回答3:

You could try the following:

+ scale_colour_manual(values = c("B" = "red"))

This will assign red to observations when value equals B.