Change the color of a subset of group labels in a boxplot in R

做~自己de王妃 提交于 2020-01-06 01:54:12

问题


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.



来源:https://stackoverflow.com/questions/28385219/change-the-color-of-a-subset-of-group-labels-in-a-boxplot-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!