[R] How to color boxplots with respect to the variable names

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Mon May 30 01:32:19 CEST 2022


Hi Neha,
As you have a distinguishing feature in the variable names, here is
one way to do it:

RF<- c(4.7, 1.52, 1.46, 4.5, 0.62, 1.12)
RF_LOO<- c(5.2, 1.52, 1.44, 4.3, 0.64, 1.11)
RF_boot<- c(5.8, 1.5, 1.23, 4.3, 0.64, 1.12)
Ranger<- c(4.5, 1.57, 1.25, 3.75, 0.56, 1.09)
Ranger_LOO<- c(5, 1.56, 1.35, 3.7, 0.6, 1.0)
Ranger_boot<- c(4.2, 1.53, 1.12, 3.7, 0.63, 1.1)
SVM<- c(3.51, 1.34, 0.62, 1.45, 0.5, 1.06)
SVM_LOO<- c(3.6, 1.33, 0.33, 1.4, 0.41, 1.1)
SVM_boot<- c(3.75, 1.35, 0.58, 1.4, 0.4, 1.0)
KNN<- c(2.85, 1.35, 0.25, 1.76, 0.43, 1.25)
KNN_LOO<- c(2.85, 1.34, 0.375, 1.75, 0.44, 1.27)
KNN_boot<- c(2.75, 1.35, 0.375, 1.75, 0.45, 1.27)
varnames<-c("RF","RF_LOO","RF_boot",
 "RANGER","RANGER_LOO","RANGER_boot",
 "SVM","SVM_LOO","SVM_boot",
 "KNN","KNN_LOO","KNN_boot")
colors<-rep("blue",length(varnames))
colors[grep("LOO",varnames)]<-"green"
colors[grep("boot",varnames)]<-"red"
at.x <- seq(1,by=.4, length.out = 10)
boxplot(RF, RF_LOO, RF_boot, Ranger, Ranger_LOO, Ranger_boot, SVM, SVM_LOO,
SVM_boot,
        KNN, KNN_LOO, KNN_boot, range = 0, col=colors, names= c("RF",
"RF_LOO", "RF_boot",
        "Ranger", "Ranger_LOO", "Ranger_boot", "SVM", "SVM_LOO", "SVM_boot",
        "KNN", "KNN_LOO",
"KNN_boot"),las=2,boxwex=0.5,outline=FALSE,cex.axis=0.8, main="Consistency
of the 100% features ")
legend(8,5.5,c("Raw","LOO","boot"),fill=c("blue","green","red"))

Jim

On Mon, May 30, 2022 at 4:46 AM Neha gupta <neha.bologna90 using gmail.com> wrote:
>
> I have the following data and I need to use a boxplot which displays the
> variables (RF, Ranger, SVM, KNN) with one color, variables (RF_boot,
> Ranger_boot, SVM_boot, KNN_boot) with another color and the variables
> (RF_LOO, SVM_LOO, Ranger_LOO, KNN_LOO) with another color.
>
> How can I do that? Currently, I am using the base boxplot which displays
> them in one color. I know it will be more easily achieved with ggplot but I
> have no experience/knowledge with it.
>
> RF= c(4.7, 1.52, 1.46, 4.5, 0.62, 1.12)
> RF_LOO= c(5.2, 1.52, 1.44, 4.3, 0.64, 1.11)
> RF_boot= c(5.8, 1.5, 1.23, 4.3, 0.64, 1.12)
> Ranger= c(4.5, 1.57, 1.25, 3.75, 0.56, 1.09)
> Ranger_LOO= c(5, 1.56, 1.35, 3.7, 0.6, 1.0)
> Ranger_boot= c(4.2, 1.53, 1.12, 3.7, 0.63, 1.1)
> SVM= c(3.51, 1.34, 0.62, 1.45, 0.5, 1.06)
> SVM_LOO= c(3.6, 1.33, 0.33, 1.4, 0.41, 1.1)
> SVM_boot= c(3.75, 1.35, 0.58, 1.4, 0.4, 1.0)
> KNN= c(2.85, 1.35, 0.25, 1.76, 0.43, 1.25)
> KNN_LOO= c(2.85, 1.34, 0.375, 1.75, 0.44, 1.27)
> KNN_boot= c(2.75, 1.35, 0.375, 1.75, 0.45, 1.27)
>
> My base boxplot is here
>
> colors = rep("blue",12)
> at.x <- seq(1,by=.4, length.out = 10)
> boxplot(RF, RF_LOO, RF_boot, Ranger, Ranger_LOO, Ranger_boot, SVM, SVM_LOO,
> SVM_boot,
>         KNN, KNN_LOO, KNN_boot, range = 0, col=colors, names= c("RF",
> "RF_LOO", "RF_boot",
>         "Ranger", "Ranger_LOO", "Ranger_boot", "SVM", "SVM_LOO", "SVM_boot",
>         "KNN", "KNN_LOO",
> "KNN_boot"),las=2,boxwex=0.5,outline=FALSE,cex.axis=0.8, main="Consistency
> of the 100% features ")
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list