【问题标题】:Change the color of a subset of group labels in a boxplot in R在R中的箱线图中更改组标签子集的颜色
【发布时间】:2015-04-07 17:50:25
【问题描述】:

如何在简单的箱线图中仅更改标签子集的颜色?例如,在 X 轴上,我有组“3”、“4”和“5”,我只想更改“4”组标签的颜色。

很容易用 col="red" 改变箱线图本身的颜色,甚至用border="red" 改变箱线图的边框,但我找不到任何关于改变标签的参考。

示例: 箱线图(mtcars$hp~mtcars$gear)

谢谢!

【问题讨论】:

    标签: r graphics plot colors label


    【解决方案1】:

    试试

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

    【讨论】:

      【解决方案2】:

      您可以尝试以下方法:

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

      当值等于 B 时,这会将红色分配给观察值。

      【讨论】:

        【解决方案3】:

        你也可以试试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"))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-17
          • 2019-06-08
          • 2018-11-30
          • 2020-08-28
          • 2020-12-26
          • 2019-12-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多