简体   繁体   中英

Circling a particular box in R boxplot

Is it possible to circle a particular box in a boxplot in R? The assumption here is that I know beforehand which of the boxes it is that I have to highlight.

I heartily second @csgillespie's suggestion to just make it a different color.

That said, I played around a bit, and this is what I came up with (using @Marc's data):

df <- data.frame(s1=rnorm(100), s2=rnorm(100, mean=2), s3=rnorm(100, mean=-2))

Plot the boxplot and keep the stats for plotting the ellipse:

foo <- boxplot(df, border=c(8,8,1), lwd=c(1,1,3))

Set semimajor and semiminor axes:

aa <- 0.5
bb <- foo$stats[4,3]-foo$stats[2,3]

Plot a parameterized ellipse around the third box:

tt <- seq(0,2*pi,by=.01)    
lines(3+aa*cos(tt),foo$stats[3,3]+bb*sin(tt))

在此输入图像描述

If you want to go with a somewhat hand drawn look and can do some interactive parts (for example, creating a presentation where one slide just shows the plot, then the next slide includes the circling of the one of interest).

  1. use the locator function to click on points that surround the part of the plot that is of interest, you might want to set type='l' so you can see the shape that you are making (but then will need to recreate the plot without the added lines)

  2. pass the return value from above to the xspline function with other options.

example:

boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
tmp <- locator(type='l')  # click on plot around box of interest
boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
xspline(tmp, open=FALSE, border='red', lwd=3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM