Creating a layer diagram with Base R plot - r

I am trying to make a layer diagram in base R with three layers:
the area under the y1 line shaded cyan,
the area between the y1 and y2 line shaded yellow and
the area between y2 and y3 shaded grey.
I tried:
x <- 1:100
y1 <- 2*x+3
y2 <- y1 +4*x+9
y3 <- y2+ 1.5*x
plot(x,y3,type="l")
polygon(x,y3,col="grey",border="grey")
polygon(x,y2,col="yellow",border="yellow")
polygon(x,y1,col="cyan",border="cyan")
but this did not work.
Thank you for your help.

You were close, the polygon needs to return to the x axis, thus we need to add coordinate [100, 0]. I show border=F, but maybe yours is better. You probably should plot lines at the end if wanted.
plot(x, y3, type="n")
polygon(c(x, 100), c(y3, 0), col="grey", border=F)
polygon(c(x, 100), c(y2, 0), col="yellow", border=F)
polygon(c(x, 100), c(y1, 0), col="cyan", border=F)
lines(x, y3, lwd=2, col="red")

Related

Using UTF-8 codes for use in intToUtf8() in R?

I'm trying to use UTF-8 code to put a symbol in some text in an R figure. If I wanted a black circle, I could use the code intToUtf8(9679). Where can I find a database that lists the values for other symbols? I want to find the code to create a red circle (i.e., pch=16, col="red"), but I can't find a list of what all of the unicode values are for specific symbols.
# example R code
x <- 1:10
y1 <- rnorm(10, x*2, 0.5)
y2 <- rnorm(10, x, 0.5)
plot(x, y1, xlab='x-value', ylab='', pch=16)
points(x, y2, pch=16, col='red')
mtext(paste0('value for y1 (', intToUtf8(9679), ') and y2 (', intToUtf8(9679), ')'), side=2, line=2)
# except that I want the second black circle in the axis label to be a red circle
Thank you for your help,
Mikey
Here is the solution I ended up using. It's not the most efficient, but it works:
x <- 1:10
y1 <- rnorm(10, x*2, 0.5)
y2 <- rnorm(10, x, 0.5)
plot(x, y1, xlab='x-value', ylab='', pch=16)
points(x, y2, pch=16, col='red')
mtext('value for y1 (\u25CF) and y2 ( )', side=2, line=2)
mtext(' \u25CF', side=2, line=2, col='red')
# alternatively, the following would also work in place of line 6
mtext(paste0('value for y1 (', intToUtf8(9679),' and y2 ( )'), side=2, line=2)
If you want to find more unicode character information for your specific symbol, you can look here.
Thank you lukeA for your help with this.

Shade the area in a PDF plot [duplicate]

I'm having trouble getting polygon() to shade below the distribution all the way to the x-axis. It seems to shade above the exponential distribution to a y=-x line. Here is where what I have so far:
x <- seq(0,50,0.01)
y <- dexp(seq(0,50,0.01),rate=0.11)
plot(x, y, type="l", col=col2rgb("yellow",0.5), xaxs="i", yaxs="i", ylim=c(0,0.15))
polygon(x, y ,border=NA,col=col2rgb("yellow",0.5))
Thanks so much!
Solution is simple, by adding (0,0) to the vertices of the polygon. See below:
x <- seq(0,50,0.01)
y <- dexp(seq(0,50,0.01),rate=0.11)
plot(x, y, type="l", col=col2rgb("yellow",0.5), xaxs="i", yaxs="i", ylim=c(0,0.15))
polygon(c(0, x), c(0, y), border=NA, col=col2rgb("yellow",0.5))
How polygon() works
polygon() will line up all vertices in order. The problem of your original code is that the origin (0, 0) is not one of the vertices, so it will not be part of the polygon. You can also consider the following toy example:
x0 <- c(0, 0.5, 1.5)
y0 <- c(1.5, 0.5, 0)
## triangle, with three vertices
plot(x0, y0, pch = ".")
polygon(x0, y0, col = "red", border = NA)
## area under triangle, four vertices
polygon(c(0, x0), c(0, y0), col = "yellow", border = NA)

How to use polygon() to shade below a probability density curve

I'm having trouble getting polygon() to shade below the distribution all the way to the x-axis. It seems to shade above the exponential distribution to a y=-x line. Here is where what I have so far:
x <- seq(0,50,0.01)
y <- dexp(seq(0,50,0.01),rate=0.11)
plot(x, y, type="l", col=col2rgb("yellow",0.5), xaxs="i", yaxs="i", ylim=c(0,0.15))
polygon(x, y ,border=NA,col=col2rgb("yellow",0.5))
Thanks so much!
Solution is simple, by adding (0,0) to the vertices of the polygon. See below:
x <- seq(0,50,0.01)
y <- dexp(seq(0,50,0.01),rate=0.11)
plot(x, y, type="l", col=col2rgb("yellow",0.5), xaxs="i", yaxs="i", ylim=c(0,0.15))
polygon(c(0, x), c(0, y), border=NA, col=col2rgb("yellow",0.5))
How polygon() works
polygon() will line up all vertices in order. The problem of your original code is that the origin (0, 0) is not one of the vertices, so it will not be part of the polygon. You can also consider the following toy example:
x0 <- c(0, 0.5, 1.5)
y0 <- c(1.5, 0.5, 0)
## triangle, with three vertices
plot(x0, y0, pch = ".")
polygon(x0, y0, col = "red", border = NA)
## area under triangle, four vertices
polygon(c(0, x0), c(0, y0), col = "yellow", border = NA)

R: how to rotate label of secondary Y axis?? {base}

I've created secondary Y axis, and I've put a label there as well using mtext. However, I can't figure out how to rotate my secondary Y label in the way to face a plot - like my red Y2 label ?
My dummy data, adopted from : http://robjhyndman.com/hyndsight/r-graph-with-two-y-axes/
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
par(mar=c(5,4,4,5)+.1)
plot(x,y1,type="l",col="red")
par(new=TRUE)
plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
mtext("y2",side=4,line=3)
legend("topleft",col=c("red","blue"),lty=1,legend=c("y1","y2"))
result:
I've tried srt = ..., of las = ..., neither of them does work.
I don't necessary need to use mtext, is there please another simple solution ?
Thank you !
Use text instead of mtext:
set.seed(1)
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
par(mar=c(5,4,4,5)+.1)
plot(x,y1,type="l",col="red")
par(new=TRUE)
plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
text(par("usr")[2]*1.11,mean(par("usr")[3:4]), "y2", srt = -90, xpd = TRUE, pos = 4)
legend("topleft",col=c("red","blue"),lty=1,legend=c("y1","y2"))
(via)

Add straight line to a plot

How can I add a straight line form (5, 5) to infinity to this plot? I use abline but I do not want the line between 5 to 5, just I need after 5 to infinity.
x <- c(1:5); y <- x
plot(x, y, type="l")
abline(v= max(x), col="black", lwd=2, lty=2)
You can use segments and par('usr') to extract the existing axis limits. I've used lty=1 to show that it wil extend to the edge of the plotting area
plot(x,y,type = 'l')
segments(x0 = max(x), y0 = max(y), y1 = par('usr')[4], lwd=2)

Resources