Field with confidence interval and identify specific data points in r

Here is my example data:

set.seed(1234) myd <- data.frame (SN = 1:100, myv = round (rnorm(100, 5, 5), 2)) boxplot(myd$myv, col = "lightgreen") 

I want to highlight the part of the box that falls into the confidence interval between 4.5 and 8.2. I also want to show how data that does not fall within the confidence interval (> 8.2 and less than 4.5). The output should look like this:

enter image description here

I just need to achieve this goal. ggplot2 or another package solution is welcome.

+4
source share
3 answers

I do not get this plot, but you can adapt to the differences.

 > boxplot(myd$myv, col = "lightgreen", ylim=c(-10,18))s 

One way to get transparent colors is to extract the RGB values ​​from the named color using col2rgb, and then go back to rgb with the corresponding scaled values:

 > col2rgb("purple") # [,1] #red 160 #green 32 #blue 240 > rect(0.7, 4.5, 1.3, 8.2, col= rgb( red=160/255, green=32/255, blu=240/255, 0.4) ) 

It is necessary to build a logical vector that can select the point values, and also be the basis for "counting" the number of such values ​​for the points function:

 > points(x=rep(1, sum( myd$myv > 8.2 | myd$myv < 4.5 )), y= myd$myv[ myd$myv > 8.2 | myd$myv < 4.5 ] , col="orange", pch=19, bg="orange") 

Built according to your specifications ...: enter image description here

+6
source

Here is the ggplot answer ggplot . Starting with Sven's answer

 interval <- c(4.5, 8.2) myd <- within(myd, group <- myv >= interval[1] & myv <= interval[2]) 

Ggplot code will be

 ggplot(myd, aes(x = 0, y = myv)) + geom_boxplot(fill = "lightgreen") + annotate("rect", ymin = interval[1], ymax = interval[2], xmin = -1/2, xmax = 1/2, fill = "blue", alpha = 0.25) + geom_point(data=myd[!myd$group,], shape = 21, colour = "black", fill = "orange") + scale_x_continuous("", breaks = NULL) + theme_bw() 

The box itself is straightforward. A bluish rectangle is an abstract. Dots are drawn on top of this, restricting the data to only those outside the range (as calculated for group ). scale_x_continuous eliminates the axial notation of x, and theme_bw() gives a simpler background and grid.

enter image description here

You can increase the number of points by pointing size to the geom_point level, and other settings are also possible.

+4
source

quick solution using gridBase

 set.seed(1234) myd <- data.frame (SN = 1:100, myv = round (rnorm(100, 5, 5), 2)) boxplot(myd$myv, col = "lightgreen") sp <- baseViewports() pushViewport(sp$plot) grid.rect(default.units='native', width= 1,height=8.2-4.5 , gp=gpar(fill=rgb(1,0,0,0.5)), y = 3) grid.points(x = rep(1,6) ,y=c(-4,-2,3,9,10,11), gp=gpar(col=rgb(1,1,0,0.5),default.units='native')) 

enter image description here

0
source

All Articles