Add an ellipse on raster plot in R - r

I need to plot an ellipse on a raster plot. I tried plotting raster with simple plot(r1) (r1 is the raster layer) and then add=T for ellipse plotting but it doesn't work. Then I tried axes=F for plotting raster and again tried add=T for ellipse. It still doesn't work.
So I tried converting the ellipse data to dataframe and try adding to raster plot.
#Creating a raster
r <- matrix(sample(1:400),20,20)
r1<-raster(r)
#Creating ellipse with given mean and standard deviation values
theta <- seq(0, 2 * pi, length=(2000))
x <- 0.2 - 0.15 * cos(theta)
y <- 0.5 - 0.15 * sin(theta)
elp<- cbind(na.omit(x),na.omit(y))
#Converting ellipse data frame (elp) to SpatialDataFrame (ps)
ps <- SpatialPolygons(list(Polygons(list(Polygon(elp)),2)))
#Plotting raster with ellipse
plot(r1)
plot(ps, add=T)
What I get is;
Ideally ps should appear as ellipse but it is a circle. On the other hand if I plot elp (data frame from which ps is created), I get an ellipse.
plot(elp)
Can someone please help with this?

Be aware that you are not creating an ellipse flattened in horizontal direction at all. As shown in the below console output, the horizontal and vertical extent of ps is almost equal.
> diff(range(x))
[1] 0.2999998
> diff(range(y))
[1] 0.2999999
I rather assume that the ellipsoid shape of the plot depicted above originates from the sizing of your plotting device. In order to create an ellipse in the first place, you are required to do something like the following (note the difference between the x and y related expansion factors).
x <- 0.2 - 0.15 * cos(theta)
y <- 0.5 - 0.05 * sin(theta)
elp <- cbind(na.omit(x),na.omit(y))
ps <- SpatialPolygons(list(Polygons(list(Polygon(elp)),2)))
Once you have a proper ellipse, you may then display it on top of the RasterLayer e.g. by using
library(latticeExtra)
spplot(r1, alpha.regions = 0.5, scales = list(draw = TRUE)) +
layer(sp.polygons(ps, col = "black", lwd = 2))

Related

Plot 3d surface or ploygon in R based on specific combinations of 3 variables

I'm trying to make a 3D scatterplot with boudaries or zones based on combinations of 3 variables that return certain values. The variables each range between 0:1, and combine to make an index that ranges from -1:1 as follows:
f(x,y,z) = (x*y)-z
I'd like to create a visual representation that will highlight all combinations of variables that return a certain index value. As an example, I can easily show those variables where index > 0 using scatterplot3d (rgl would also work):
# Create imaginary dataset of 50 observations for each variable
x<-runif(50,0,1)
y<-runif(50,0,1)
z<-runif(50,0,1)
# Create subset where f(x,y,z) > 0
x1<-y1<-z1<-1
for (i in 1:length(x)){ if ((x[i]*y[i])-z[i] > 0) {
x1<-rbind(x1, x[i])
y1<-rbind(y1, y[i])
z1<-rbind(z1, z[i])}
}
s3d<-scatterplot3d(x,y,z) # Plot entire dataset
s3d$points3d(x1,y1,z1,pch=19, col="red") # Highlight subset where f(x,y,z) > 0
This gives me the following graph:
It seems fairly intuitive that there should be an easy way to plot either the surface (extending from top/right/back to bottom/left/front) separating the subset from the rest of the data, or else a volume/3D area within which these plots lie. E.g. my first instinct was to use something like surface3d, persp3d or planes3d. However, all attempts so far have only yielded error messages. Most solutions seem to use some form of z<-lm(y~x) but I obviously need something like q<-func((x*y)-z) for all values of x, y and z that yield q > 0.
I know I could calculate extreme points and use them as vertices for a 3D polygon, but that seems too "manual". It feels like I'm overlooking something fairly simple and obvious. I've looked at many similar questions on Stack but can't seem to find one that fits my particular problem. If I've missed any and this question has been answered already, please do point me in the right direction!
Here is a suggestion for an interactive 3D plot that is based on an example from the "R Graphics Cookbook" by Winston Chang.
set.seed(4321)
library(rgl)
interleave <- function(v1,v2) as.vector(rbind(v1,v2))
x <- runif(50)
y <- runif(50)
z <- runif(50)
plot3d(x, y, z, type="s", size=0.6, col=(2+(x*y<z)))
x0 <- y0 <- seq(0, 1, 0.1)
surface3d(x0, y0, outer(x0, y0), alpha=0.4) #plot the surface f(x,y)=x*y
x1 <- x[x * y > z] #select subset that is below the separating surface
y1 <- y[x * y > z]
z1 <- z[x * y > z]
segments3d(interleave(x1, x1), #highlight the distance of the points below the surface
interleave(y1, y1),
interleave(x1 * y1, z1), col="red", alpha=0.4)
If you don't like the red lines and only want the surface and the colored points, this will be sufficient:
plot3d(x,y,z,type="s",size=0.6,col=(2+(x*y<z)))
x0 <- y0 <- seq(0,1,0.1)
surface3d(x0,y0,outer(x0,y0),alpha=0.4)
Does this representation provide the information that you wanted to highlight?
The first thought was to see if the existing functions within scatterplot3d could handle the problem but I think not:
my.lm <- lm(z ~ I(x) * I(y)+0)
s3d$plane3d(my.lm, lty.box = "solid", col="red")
pkg:scatterplot3d doesn't really have a surface3d function so you will need to choose a package that provides that capability; say 'rgl', 'lattice', or 'plot3d'. Any of them should provide the needed facilities.

Inputting a fixed colour input vector with RGL in R

Is there any way to input a fixed vector of colours to any 3D rgl plots? If so it would be possible to extrude a map tile to a 3D surface based on a raster of the same area. But I'm finding the surface3d function behaves the same as raster::plot by insisting on mapping the input colour vector to the z variable. Is this beyond rgl's functionality at present?
I don't actually know if what you say about the coloring is correct for all rgl coloring functions, but it is not correct for rgl.surface(). This is a corruption of the example on the ?rgl.surface page. The color vector index was formed from the x-y (actually x-z) coordinates and gives a striping effect because they were modulo-ized to pull values from from a limited range.
library(rgl)
data(volcano)
y <- 2 * volcano
x <- 10 * (1:nrow(y))
z <- 10 * (1:ncol(y))
ylim <- range(y)
ylen <- ylim[2] - ylim[1] + 1
colorlut <- terrain.colors(ylen)
col <- colorlut[(x+length(x)*y +1)%%ylen ]
rgl.open()
rgl.surface(x, z, y, color=col, back="lines")
rgl.snapshot("striped_volcano.png")

How to specify z axis range and add add circle or ellipse in 3D plot in R

3-D graphing with Google(http://www.r-bloggers.com/3-d-graphing-with-google/)
(mu1=0 mu2=0 sigma1=1 sigma2=1 pho=0)
exp((-1/2)*(x^2+y^2))/(2*pi) from -3 to 3
The rotate plot will be showd from google. The profile was a circle.
Dear Prof. Bolker gave me the R code:
library("emdbook")
library("rgl")
curve3d(dmvnorm(c(x,y),mu=c(0,0),Sigma=diag(2)),
sys3d="rgl",front="line",back="line",
xlim=c(-3,3),ylim=c(-3,3))
How to specify z axis range and get the plot like google's plot?
If pho=0 then the profile parallel to XY plane was circle.If pho<>0 then the profile parallel to XY plane was ellipse. How to add circle or ellipse in 3D plot? Thanks.
I am not sure that I fully understand your question but:
1/ I do not think rgl allow to specify z axis range (and curve3d seems to allow it only for xlim, ylim) so you probably need to do it by hand
2/ You can rescale axis in rgl using rgl.viewpoint : e.g., rgl.viewpoint(scale=c(1,1,0.1))
3/ You can draw circle or ellipse using:
t <- matrix(seq(-pi/2,pi/2, len=50), 50, 50, byrow=TRUE)
p <- matrix(seq(-pi, pi, len=50), 50, 50)
r <- 10
x <- r*cos(t)*cos(p)
y <- r*cos(t)*sin(p)
z <- r*sin(t)
persp3d(x, y, z)

Draw an ellipse based on its foci

Is there a way to draw a simple ellipse based on the following definition (instead of eigenvalue) in R?
The definition I want to use is that an ellipse is the set of points in a plane for which the sum of the distances to two fixed points F1 and F2 is a constant.
Should I just use a polar cordinate?
This may be more algorithmic question.
As #DWin suggested, there are several implementations for plotting ellipses (such as function draw.ellipse in package plotrix). To find them:
RSiteSearch("ellipse", restrict="functions")
That being said, implementing your own function is fairly simple if you know a little geometry. Here is an attempt:
ellipse <- function(xf1, yf1, xf2, yf2, k, new=TRUE,...){
# xf1 and yf1 are the coordinates of your focus F1
# xf2 and yf2 are the coordinates of your focus F2
# k is your constant (sum of distances to F1 and F2 of any points on the ellipse)
# new is a logical saying if the function needs to create a new plot or add an ellipse to an existing plot.
# ... is any arguments you can pass to functions plot or lines (col, lwd, lty, etc.)
t <- seq(0, 2*pi, by=pi/100) # Change the by parameters to change resolution
k/2 -> a # Major axis
xc <- (xf1+xf2)/2
yc <- (yf1+yf2)/2 # Coordinates of the center
dc <- sqrt((xf1-xf2)^2 + (yf1-yf2)^2)/2 # Distance of the foci to the center
b <- sqrt(a^2 - dc^2) # Minor axis
phi <- atan(abs(yf1-yf2)/abs(xf1-xf2)) # Angle between the major axis and the x-axis
xt <- xc + a*cos(t)*cos(phi) - b*sin(t)*sin(phi)
yt <- yc + a*cos(t)*sin(phi) + b*sin(t)*cos(phi)
if(new){ plot(xt,yt,type="l",...) }
if(!new){ lines(xt,yt,...) }
}
An example:
F1 <- c(2,3)
F2 <- c(1,2)
plot(rbind(F1, F2), xlim=c(-1,5), ylim=c(-1, 5), pch=19)
abline(h=0, v=0, col="grey90")
ellipse(F1[1], F1[2], F2[1], F2[2], k=2, new=FALSE, col="red", lwd=2)
points((F1[1]+F2[1])/2, (F1[2]+F2[2])/2, pch=3)

Plotting a 3D surface plot with contour map overlay, using R

I have a 3-tuple data set (X,Y,Z points) that I want to plot using R.
I want to create a surface plot from the data, and superimpose a contour map on the surface plot, so as to create the impression of the contour map being the "shadow" or projection from the surface plot. The contour map is to appear below the surface plot.
My data set looks somewhat like this:
Axis | Data Type
-------------------
X | Date value
Y | Float value
Z | Float value
How can I achieve this?
Edit:
I just saw that you pointed out one of your dimensions is a date. In that case, have a look at Jeff Ryan's chartSeries3d which is designed to chart 3-dimensional time series. Here he shows the yield curve over time:
Original Answer:
As I understand it, you want a countour map to be the projection on the plane beneath the 3D surface plot. I don't believe that there's an easy way to do this other than creating the two plots and then combining them. You may find the spatial view helpful for this.
There are two primary R packages for 3D plotting: rgl (or you can use the related misc3d package) and scatterplot3d.
rgl
The rgl package uses OpenGL to create interactive 3D plots (read more on the rgl website). Here's an example using the surface3d function:
library(rgl)
data(volcano)
z <- 2 * volcano # Exaggerate the relief
x <- 10 * (1:nrow(z)) # 10 meter spacing (S to N)
y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W)
zlim <- range(z)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen,alpha=0) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
open3d()
rgl.surface(x, y, z, color=col, alpha=0.75, back="lines")
The alpha parameter makes this surface partly transparent. Now you have an interactive 3D plot of a surface and you want to create a countour map underneath. rgl allows you add more plots to an existing image:
colorlut <- heat.colors(zlen,alpha=1) # use different colors for the contour map
col <- colorlut[ z-zlim[1]+1 ]
rgl.surface(x, y, matrix(1, nrow(z), ncol(z)),color=col, back="fill")
In this surface I set the heights=1 so that we have a plane underneath the other surface. This ends up looking like this, and can be rotated with a mouse:
scatterplot3d
scatterplot3d is a little more like other plotting functions in R (read the vignette). Here's a simple example:
temp <- seq(-pi, 0, length = 50)
x <- c(rep(1, 50) %*% t(cos(temp)))
y <- c(cos(temp) %*% t(sin(temp)))
z <- c(sin(temp) %*% t(sin(temp)))
scatterplot3d(x, y, z, highlight.3d=TRUE,
col.axis="blue", col.grid="lightblue",
main="scatterplot3d - 2", pch=20)
In this case, you will need to overlay the images. The R-Wiki has a nice post on creating a tanslucent background image.

Resources