how to add a transparent rectangle to a R boxplot (plot)?

Leo... picture Leo... · Aug 5, 2013 · Viewed 11.9k times · Source

I have a simple boxplot for my data using R ..

boxplot (Error~Code, DataFrame1, xlim = c(0, 27),
     xlab="set Code",ylab="Error", boxwex=0.75, cex.axis=0.3)

and I would like to draw a transparent rectangle all over the plot between 2 defined y-values: (-50 ) and (100)!

I tried using the function rect as follows after the previous script:

  rect(0,-50,27,100, col= 'tomato2', density=10)

but this does not give me a uniform colored rectangle with transparency!!

Could anybody please help me in that? I almost spend over 2 hours until now on this with no success.

Many thanks in advance!

Answer

Mark Lakata picture Mark Lakata · Jan 5, 2017

density will result in cross hatching, which is not what you want. What you want is alpha blending.

Try

# arguments to rgb(r,g,b and alpha) should be between 0 and 1.0
# this will make transparent blue
rect(x0,y0,x1,y1, col= rgb(0,0,1.0,alpha=0.5))