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)
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?