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

13,680

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))
Share:
13,680

Related videos on Youtube

Leo...
Author by

Leo...

PhD-student ...

Updated on June 04, 2022

Comments

  • Leo...
    Leo... almost 2 years

    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!

    • user20650
      user20650 almost 11 years
      Define the colour using hex. eg rect(0,-50,27,100 ,col= '#FF003322'). The first 6 numbers give the red colour and the last two the level of transparency (i think)