Create a rectangle in R that sticks the box with the sample size (N)

Is there a way to create a rectangle in R that will be displayed with the field (somewhere) "N = (sample size)"? The varwidth logic adjusts the width of the window based on the sample size, but this does not allow comparing different graphs.

FWIW, I use the boxplot command as follows, where "f1" is a factor:

boxplot(xvar ~ f1, data=frame, xlab="input values", horizontal=TRUE) 
+12
r graph plot label boxplot
Aug 14 '10 at 12:12
source share
5 answers

Here is the ggplot2 code. It will display the sample size with a middle space, making the tag multifunctional!

First, a simple function for fun.data

 give.n <- function(x){ return(c(y = mean(x), label = length(x))) } 

Now, to showcase diamond data

 ggplot(diamonds, aes(cut, price)) + geom_boxplot() + stat_summary(fun.data = give.n, geom = "text") 

You may have to play with the size of the text to make it look good, but now you have a label for the sample size, which also gives a skew feel.

+23
Aug 14 2018-10-14
source share

You can use the names parameter to write n next to each factor name.

If you don't want to compute n yourself, you can use this little trick:

 # Do the boxplot but do not show it b <- boxplot(xvar ~ f1, data=frame, plot=0) # Now b$n holds the counts for each factor, we're going to write them in names boxplot(xvar ~ f1, data=frame, xlab="input values", names=paste(b$names, "(n=", b$n, ")")) 
+9
Aug 14 '10 at 12:59
source share

To get n on top of the panel, you can use text with the stat data represented by boxplot as follows

 b <- boxplot(xvar ~ f1, data=frame, plot=0) text(1:length(b$n), b$stats[5,]+1, paste("n=", b$n)) 

Statistic field b means a matrix, each column contains the extreme part of the lower whisker, the lower loop, the median, the upper loop and the extreme part of the upper niche for one group / plot.

+4
Nov 21 '14 at 18:57
source share

The gplots package provides boxplot.n , which, according to the documentation, creates a notebook annotated with the number of observations.

0
Jun 27 '13 at 10:59 on
source share

I figured out a workaround using the Envstats package. This package must be downloaded, downloaded and activated using:

 library(Envstats) 

stripChart (different from stripchart) adds some values ​​to the chart, such as n values. First I built my box. Then I used add = T in stripChart. Obviously, many things were hidden in the stripChart code so that they would not appear on the box. Here is the code I used for stripChart to hide most of the elements.

Boxplot with integrated stripChart to display n values:

 stripChart(data.frame(T0_G1,T24h_G1,T96h_G1,T7d_G1,T11d_G1,T15d_G1,T30d_G1), show.ci=F,axes=F,points.cex=0,n.text.line=1.6,n.text.cex=0.7,add=T,location.scale.text="none") 

So boxplot

 boxplot(data.frame(T0_G1,T24h_G1,T96h_G1,T7d_G1,T11d_G1,T15d_G1,T30d_G1),main="All Rheometry Tests on Egg Plasma at All Time Points at 0.1Hz,0.1% and 37 Set 1,2,3", names=c("0h","24h","96h","7d ", "11d", "15d", "30d"),boxwex=0.6,par(mar=c(8,4,4,2))) 

Then stripChart

 stripChart(data.frame(T0_G1,T24h_G1,T96h_G1,T7d_G1,T11d_G1,T15d_G1,T30d_G1), show.ci=F,axes=F,points.cex=0,n.text.line=1.6,n.text.cex=0.7,add=T,location.scale.text="none") 

You can always adjust the height of the numbers (n values) to suit you.

0
Nov 10 '16 at 9:37
source share



All Articles