[R] Boxplot: draw outliers in colours

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Fri Sep 28 13:14:27 CEST 2018


Hi Luigi,
An easy way is to use "points" to overplot the outliers:

grbxp<-boxplot(dfA$Y ~ dfA$X,
         ylim=c(0, 200),
         col="green",
         ylab="Y-values",
         xlab="X-values"
 )
points(grbxp$group,grbxp$out,col="green")


On Fri, Sep 28, 2018 at 7:51 PM Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
>
> Dear all,
> I am trying to overlap two series of boxplots on the same graph. In
> order to distinguish the outliers from one series to the other, would
> be possible to colour the outliers?: instead of the standard black, is
> it possible to give a chosen colour?
> Thank you
>
> >>>
> This is the example. I could not generate not normally distributed
> random values (even with runif), so I had to create the values
> manually and they are still a bit rough (in the real thing, the two
> series are more distant), anyway this should better explain the case.
> Note that the three outliers at the bottom right belong to the 'blue'
> distribution and the upper outlier on the right belongs to the 'green'
> distribution, so making them in blue and green colours would make
> clearer their positions.
>
> # generate data.frames
> A = c(70, 22, 4, 21, 29, 35, 24, 20, 9, 21,
>       22, 12, 20, 21, 13, 18, 15, 3, 9, 23,
>       6, 5, 2, 24, 25, 21, 16, 0, 4, 1)
> B = c(17, 21, 70, 6, 23, 10, 8, 5, 22, 5,
>       21, 5, 19, 9, 23, 24, 11, 13, 7, 15,
>       25, 9, 13, 14, 11, 9, 12, 0, 5, 9)
> C = c(17, 8, 30, 22, 11, 32, 33, 8, 160, 11,
>       35, 7, 36, 15, 11, 25, 16, 6, 38, 19,
>       35, 30, 12, 27, 22, 32, 47, 39, 31, 26)
> D = c(79, 26, 8, 33, 59, 67, 60, 65, 54, 88,
>       78, 105, 59, 40, 109, 81, 28, 26, 94,
>       35, 10, 38, 58, 79, 58, 10, 5, 8, 4, 50)
> E = c(98, 104, 101, 102, 97, 97, 97,
>       100, 97, 102, 100, 103, 104,
>       104, 99, 102, 100, 97, 102, 105,
>       99, 105, 100, 102, 100, 115,
>       112, 113, 111, 115)
> G = c(105, 130, 97, 105, 113, 123,
>       149, 15,     134, 148, 98, 104,
>       113, 108, 209, 145, 138, 119,
>       142, 129, 298, 101, 136, 129,
>       148, 295, 125, 277, 107, 642)
> X = rep(c(1, 3, 5),30*3)
> dfA <- data.frame(X, c(A, B, C))
> dfB <- data.frame(X, c(D, E, G))
> names(dfA) <- c("X", "Y")
> names(dfB) <- c("X", "Y")
>
> # plot
> boxplot(dfA$Y ~ dfA$X,
>         ylim=c(0, 200),
>         col="green",
>         ylab="Y-values",
>         xlab="X-values"
> )
> par(new=TRUE)
> boxplot(dfB$Y ~ dfB$X,
>         ylim=c(0, 200),
>         col="blue",
>         ylab="", xlab="",
>         xaxt="n", yaxt="n"
> )
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> 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