Boxplots - rstudio

Asked

Viewed 634 times

1

I intended to color the various boxplots underlying the code I made (I leave here) but I’m having some difficulty. If it is possible for someone to give some info on how to do it, thank you. Thank you

> a<-c(0, 15, 10, 10)
> b<-c(16,21,2,14)
> c<-c(30,3,11)
> d<-c(0, 14, 18, 3)
> e<-c(27,44,0)
> f<-c(33,2)
> g<-c(1, 1, 0)
> h<-c(32,3)
> i<-c(2,1,0)
> j<-c(31,4)
> k<-c(3,1,0)
> l<-c(0,18,16,1)
> m<-c(0,13,20,2)
> n<-c(0,16,17,2)
> o<-c(32,3)
> p<-c(2,1)
> q<-c(1,0,20,14)
> r<-c(12,23)
> s<-c(6,16,13,0,0)
> t<-c(7,16,12,0,0)
> u<-c(7,15,13,0,0)
> v<-c(0,19,16)
> x<-c(0,17,18)
> z<-c(0,13,17,5)
> w<-c(27,8,0,0)
boxplot(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,x,z,w) 

2 answers

3

First we have to know how many boxes there are in this chart. Just copy the instruction boxplot and replace the graphical command with length(list(etc)).

length(list(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,x,z,w))
#[1] 25

Now, just use the argument col with an appropriate value.

boxplot(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,x,z,w, col = rainbow(25)) 

inserir a descrição da imagem aqui

There are several other ways to choose colors. I suggest you start with help("rainbow") and follow the links there.

1

You can use the code below to color the boxes. But you can also use more robust libraries like ggplot or ggplot2. In the example below each box was colored individually, as well as the border. Just follow the example for all variables.

   boxplot(
   a,
   col = "lightgray",
   border=c("blue"),
   ylim = c(0, 45),
   xlim = c(0, 25),
   at = 1:1 
 )
 boxplot(
   b,
   col = "lightgray",
   border=c("green"),
   ylim = c(0, 45),
   xlim = c(0, 25),
   at = 1:1 + 1,
   add = TRUE
 )
 boxplot(
   c,
   col = "lightgray",
   border=c("red"),
   ylim = c(0, 45),
   xlim = c(0, 25),
   at = 1:1 + 2,
   add = TRUE
 )
 boxplot(
   d,
   col = "lightgray",
   border=c("blue"),
   ylim = c(0, 45),
   xlim = c(0, 25),
   at = 1:1 + 3,
   add = TRUE
 )

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.