Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shading certain area in graph - a combination of lines and point plots in base R

Tags:

graph

r

base

I have following question:

set.seed(1234)
dis = seq(1, 800, 10)
 yvar = rnorm(length (dis), 50, 10)
myd <- data.frame (xvar = dis, yvar = yvar, yvarL = 
yvar - rnorm (length (yvar),5, 1), yvarU = yvar + rnorm (length (yvar), 5, 1))
plot(myd$xvar,myd$yvar,pch=19,cex=.75,col="blue")
points(myd$xvar,myd$yvarL,col="gray", pch=19,cex=.5)
points(myd$xvar,myd$yvarU,col="gray", pch=19,cex=.5)

Y has three values - Y, upper and lower confidence. I would like to shade between two intervals in axis. Rest of graph would be point only. The hypothesized product graph should be similar (not exactly).

enter image description here

How can I shade and produce line plot between two intervals say between 650 to 690 and 210 to 280 in myd$xvar. I tried to do some polygot fitting based on this post ( shading within xy curve plot in R), but not successful.

polygon(c(myd$xvar[1:200], myd$xvar[200:1]), c(myd$xvar[1:200],
   myd$yvar[200:1]), col="gray")
like image 669
fprd Avatar asked Oct 24 '12 15:10

fprd


People also ask

How do I shade an area under a graph in R?

The polygon function can be used to shade the area under the density curve. You just need to pass the density object to it and specify a color.

How do you shade a graph in R?

Method 1: Shade a graph using polygon function : In this method, we are calling the polygon function with its argument col passed with the name or the hex code of the color needed to be shaded in the given graph.

How do you shade a normal distribution curve in R?

The easiest-to-find method for shading under a normal density is to use the polygon() command. That link is to the first hit on Google for “Shading Under a Normal Curve in R.” It works (like a charm), but it is not the most intuitive way to let users produce plots of normal densities.


1 Answers

One problem with your data is that myd has only 80 observations, but you are trying to plot 200... Just feed the x variable twice into polygon() (once as-is, once in reverse order), then the y variable (first the lower limit as-is, then the upper limit in reverse order). Then you have your shading:

polygon(c(myd$xvar[20:40], myd$xvar[40:20]),c(myd$yvarL[20:40],
 myd$yvarU[40:20]), col="gray")

However, the shading will cover some points, so you will have to re-plot them:

points(myd$xvar,myd$yvar,pch=19,cex=.75,col="blue")
points(myd$xvar,myd$yvarL,col="gray", pch=19,cex=.5)
points(myd$xvar,myd$yvarU,col="gray", pch=19,cex=.5)

EDIT: rfan below asks to shade based on the x coordinate, not the index, and to add a line in the shaded region. For this, it makes sense to extract an index of the relevant points (we use that myd$xvar is sorted):

index <- which(myd$xvar>=200 & myd$xvar<=400)

We then plot based on this index, which we can easily modify if we need to:

polygon(c(myd$xvar[index], myd$xvar[rev(index)]),c(myd$yvarL[index], 
 myd$yvarU[rev(index)]), col="gray")

Now, add a line and the additional points:

lines(myd$xvar[index],myd$yvar[index],pch=19,cex=.75,col="blue",type="o")
points(myd$xvar,myd$yvarL,col="gray", pch=19,cex=.5)
points(myd$xvar,myd$yvarU,col="gray", pch=19,cex=.5)
like image 140
Stephan Kolassa Avatar answered Sep 30 '22 16:09

Stephan Kolassa