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

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)

Related

How to draw a half circle on a plot?

I am trying to draw some half circles on a plot, using the trigonometric functions in R.
So here is what I have :
matPoints <<- as.data.frame(cbind(X=c(-1, -(sqrt(3)/2), -(sqrt(2)/2), -0.5, 0, 0.5, sqrt(2)/2, sqrt(3)/2, 1), Y=c(0, 0.5, sqrt(2)/2, sqrt(3)/2, 1, sqrt(3)/2, sqrt(2)/2, 0.5, 0)))
plot(x = matPoints$X*W, y = matPoints$Y*W)
For the moment, it prints each point on the plot. What I want to do here is to trace a smooth line between points so it gives me a beautiful half circle of center (0, 0) and of scale W.
Any solution?
Do you mean this?
x <- seq(0, pi, length.out = 500)
W <- 3
plot(cos(x) * W, sin(x) * W, type = "l")
Since the general equation of a circle is x^2 + y^2 = r^2, you can mimic it like below as well,
r=3 # radius
x <- seq(-r,r,0.01)
y <- sqrt(r^2 - x^2)
plot(x,y,type="l")
# plot(c(x,x),c(y,-y),type="l") for a full circle.
gives,
Here's another possibility using complex numbers, and polygon to draw a closed shape.
plot(NA, xlim=c(-2,2), ylim=c(-2,2))
polygon(1i^(seq(0,2,l=100)))
Using this method you can easily change the centre, scale, rotation, fill colour etc:
plot(NA, xlim=c(-2,2), ylim=c(-2,2))
polygon(2*(1i^(seq(0,2,l=100)))*1i^.5 + .1-.3i, col="red")
polygon(1i^(seq(0,2,l=100)), col="blue")

Making plot abline lines transparent

I would like to use the function abline to plot several lines which should be made transparent but I am stuck.
Currently I have this code:
plot(x=NA, type="n", ylim=c(1, 9), xlim=c(1, 4), xlab="x-value",
ylab="y-value", cex.axis = 1, cex.lab = 1)
for (i in 1:nrow(one_hundred_regressions)) {abline(coef=one_hundred_regressions[i,],
col = "red")}
Use adjustcolor changing the alpha value to whatever degree of transparency you want:
plot(1:10)
abline(v = 1:10, col = adjustcolor("red", alpha = 0.3))
You may use RGB colors "#RRGGBBaa", where aa is the alpha value for transparency. Example:
plot(1:10, type="n")
Map(function(x, y, z) abline(coef=c(x, y), col=z, lwd=2), 2:4, 2/(2:4)^2,
c("#AA000066", "#00AA0066", "#0000AA66"))

How to add confidence intervals to 3D surface?

I have a matrix 40x40 from values obtained by interpolation using library akima to create a 3D surface.
I estimated CI 95% using monte carlo simulations from predicted values and now I want to add them for year 0 to my 3D graph.
I’m doing something wrong and I don’t understand how to plot vertical lines to represent the CIs.
My lines look like this:
And I want to have CI like on this image:
Here's are my data, dropbox link because it's longer than the space allowed to post here: https://www.dropbox.com/s/c6iyd2r00k5jbws/data.rtf?dl=0
and my code:
persp(xyz,theta = 45, phi = 25,border="grey40", ticktype = "detailed", zlim=c(0,.8))->res2
y.bin <- rep(1,25)
x.bin <- seq(-10,10,length.out = 25)
points (trans3d(x.bin, y.bin, z = y0, pmat = res2), col = 1, lwd=2)
lines (trans3d(x.bin, y.bin, z = LCI, pmat = res2), col = 1, lwd=2)
lines (trans3d(x.bin, y.bin, z = UCI, pmat = res2), col = 1, lwd=2)
The problem is that the upper and lower confidence intervals are being drawn as a single line. If you loop over the points with an interval, and then plot the line between the upper and lower values there, the plot looks closer to what you want. Note that point values in the example y0 is not on many of the 3d intervals.
# data from link is imported
persp(xyz,theta = 45, phi = 25,border="grey40", ticktype = "detailed", zlim=c(0,.8))->res2
y.bin <- rep(1,25)
x.bin <- seq(-10,10,length.out = 25)
# y0 points
points (trans3d(x.bin, y.bin, z = y0, pmat = res2), col = 1, lwd=2)
# lines between upper and lower CIs for each location
for(i in 1:length(LCI)){
lines (trans3d(rep(x.bin[i],2), rep(y.bin[i],2), z = c(LCI[i],UCI[i]), pmat = res2), col = 1, lwd=2)
}

Changing the distance or rotation of the axis title of a graph relative to the axis itself with plot3D

I draw the graph below using the plot3D package of R. I would like to move a bit the z-axis label ($ f (y1, y2)) off the axis values or leave it in horizontal. Does anyone know how I can proceed? Next the code:
library("plot3D")
#Function density probability
library(pbivnorm)
bsb <- function(t1,t2){
a1 <- sqrt(phi1/2)*(sqrt(((phi1+1)*t1)/(phi1*mu1))-sqrt(((phi1*mu1)/((phi1+1)*t1))))
a2 <- sqrt(phi2/2)*(sqrt(((phi2+1)*t2)/(phi2*mu2))-sqrt(((phi2*mu2)/((phi2+1)*t2))))
Phi2 <- pbivnorm(a1, a2, rho, recycle = TRUE)
b1 <- ((phi1+1)/(2*phi1*mu1))*sqrt(phi1/2)*(((phi1*mu1)/((phi1+1)*t1))^(1/2)+((phi1*mu1)/((phi1+1)*t1))^(3/2))
b2 <- ((phi2+1)/(2*phi2*mu2))*sqrt(phi2/2)*(((phi2*mu2)/((phi2+1)*t2))^(1/2)+((phi2*mu2)/((phi2+1)*t2))^(3/2))
fdp <- Phi2*b1*b2
return(fdp)
}
t1 <- seq(0.001,100,length=30)
t2 <- seq(0.001,20,length=40)
#Parameters
mu1=7
phi1=2
mu2=1
phi2=9
rho=0
z<-outer(t1,t2,bsb) # calculate density values
pmat=persp3D(t1, t2, z,
main="",xlab="$y_{1}$",ylab="$y_{2}$",zlab="$f(y_{1},y_{2})$",cex.axis=1,cex.lab=1,
col = "gray10",border = "gray40",
theta=50, phi=15,
expand=0.9,
d=2,
shade=0.3,
ticktype="detailed",
nticks=5,
facets=FALSE,contour = list(nlevels=10,col="gray35"), zlim= c(-0.1, 0.23), bty = "b2")
text(trans3d(0,7.3,0.173,pmat), "(b)",cex=1,col="black")
The picture looks like this:
I would like the following pictures:
The problem is with the sizing of your output window. If you use a larger one, the text will look better. For example, with a full screen window I get this from your original code:
You could also specify a lower dpi value when you open the window if your graphics device supports that. For example, if I use dev.new(dpi = 50) I get
I don't think there's a way to rotate labels in persp3D, but you can draw the plot with no label, then use text to add a label. You'll also need to increase the margin size on that side. For example,
par(mar = c(5.1, 9.1, 4.1, 2.1))
pmat <- persp3D(t1, t2, z, main="", xlab="$y_{1}$", ylab="$y_{2}$",
zlab="", cex.axis=1, cex.lab=1,
col = "gray10", border = "gray40",
theta=50, phi=15,
expand=0.9, d=2, shade=0.3,
ticktype="detailed", nticks=5,
facets=FALSE, contour = list(nlevels=10,col="gray35"),
zlim= c(-0.1, 0.23), bty = "b2")
text(trans3d(0,7.3,0.173,pmat), "(b)", cex=1, col="black")
text(trans3d(0,-3,0.05,pmat), label= "$f(y_{1},y_{2})$",
cex=1, col="black", xpd=NA, pos=2)
This gives me:

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)

Resources