【问题标题】:How to assign colors to boxplot in R? Can only use base R如何在 R 中为箱线图分配颜色?只能使用基础 R
【发布时间】:2021-03-03 17:51:33
【问题描述】:

我在尝试为 R 中的箱线图分配颜色时遇到问题。这是我尝试运行代码时的代码和输出。真的不知道该怎么办

region1<-c("North-Apulia","Calabria","South-Apulia","Sicily")
region2<-c("Inland-Sardinia,","Coast-Sardinia")
region3<-c("Umbria","East-Liguria","West-Liguria")
# c - Boxplots
mycolors<-ifelse(levels(betterolivelabel)==region1,col="red",
                 ifelse(levels(betterolivelabel)==region2,col="blue",
                        ifelse(levels(betterolivelabel)==region3, col="green")))```


```Error in ifelse(levels(betterolivelabel) == region1, col = "red", ifelse(levels(betterolivelabel) ==  : 
  unused argument (col = "red")```
> 

【问题讨论】:

    标签: r plot graph colors boxplot


    【解决方案1】:

    当您要比较多个值时,请使用%in%。当不满足任何条件时,您还需要提供最终值。

    lvls <- levels(betterolivelabel)
    
    mycolors<-ifelse(lvls %in% region1,"red",
                     ifelse(lvls %in% region2,"blue",
                            ifelse(lvls %in% region3, "green", "black")))
    

    【讨论】:

      【解决方案2】:

      我们可以使用case_when

      library(dplyr)
      lvls <- levels(betterolivelabel)
      mycolors <- case_when(lvls %in% region1 ~ "red",
                            lvls %in% region2 ~ "blue",
                            lvls %in% region3 ~ "green",
                            TRUE ~ "black")
      

      【讨论】:

        猜你喜欢
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-06
        相关资源
        最近更新 更多