I have a simple raster, that running through the filledContour function in the raster package, to give a filled contour plot.
I am trying to overlay the outline of a polygon over the top of this filled contour plot but having trouble.
reading the notes in filled.contour - adding lines to a filled contour plot has to be done in the plot.axis section, but not having any luck.
So far i have something like this:
library(raster)
library(rgdal)
test_raster <- raster("test_raster.grd")
test_Shapefile <- readOGR("test_shapefile.shp")
test_filled_contour <- filledContour(test, maxpixels=100000, plot.axes = {
axis(1)
axis(2)
map(test_Shapefile, add=T)
})
Both the raster and shapefile use the same coordinate system.
Any help welcome, i can provide the data if needed.
Related
I'm making a raster data ploting in R, when I adjust to the area I'm working on, R displays the ylim that doesn't want to be cut off.
I tried:
# set lon lat
ylim=c(-4,2)
xlim=c(118,126)
plot(pm10_mean,xlim=xlim, ylim=ylim)
plot(shp, add=TRUE)
plot(shp2, add=TRUE)
but I got picture like this
How to remove free space above 2 and below -4? I just want to plot area in xlim and ylim
I think spplot works fine but I find it really slow and hard to customize.
I found a more recent duplicate of this question in gis.stackexchange and provided the below answer:
I found that raster::plot's documentation indicates that you can specify the plotting window and in my experience it has zero effect. Instead it will always assume the extent of the raster you give it.
My suggested workaround is to first draw your desired plot window with a blank object then add the raster plot to this window.
my_window <- extent(-4, 2, 118, 126)
plot(my_window, col=NA)
plot(my_raster, add=T)
For me, this achieves what I'm after. The caveat is if you're plotting a stack or brick, the add functionality doesn't work in my experience. The solution is to use subset, like plot(subset(my_brick,4), add=T)
I have had this problem before. You can manually resize the plot area to remove the blank areas, or insert polygons to cover the unwanted areas of shapefile. But the safest option is to use spplot as this will automatically resize the plotting area for you:
require(maptoolS)
require(raster)
data(wrld_simpl)
rs=raster()
rs[]=1
id_shp=wrld_simpl[which(wrld_simpl$ISO2=="ID"),]
rs=crop(rs,id_shp)
rs=disaggregate(rs,40)
rs=mask(rs,id_shp)
spplot(rs,ylim=c(-4,2),xlim=c(118,126),sp.layout=list('sp.lines', id_shp, lwd=2,first=F))
Just as easy to simply plot a NULL plot and add your raster
plot(NULL, xlim = c(-114.5, -35.5), ylim = c(-24.5, 29.5), asp = 30/30,
xlab = "Longitude", ylab = "Latitude")
plot(your_raster, add = T)
Stacking still seems to work
i am trying to plot same shapefile over multiple raster plot using the code below:
ras <- list.files("filepath", pattern = "\\.tif$", full=TRUE)
s <- stack(ras)
plot(s)
plot(world, add=TRUE)
Unfortunately, adding shapefile 'world' using the code below doesn't seem to work.
plot(world, add=TRUE)
Any help would be appreciated.
Thanks
I used to plot lots of rasters, and would plot data points, and overlay a map of the world to visualize my results.
I would plot my points first, and then add the map. This sounds like what you want to do
I did not use the plot command twice though. I would try
plot(s)
map(region="world", add=T)
Because I don't have access to your data, I cannot test this myself
I'd like to add the boundary lines for Scotland into a filledContour plot I have of a raster file. so my code currently looks like:
filledContour(neap_hu3, levels=seq(-1,8,0.25), col=rgb.palette(36),
plot.title = title(xlab="Longitude", ylab="Latitude"),
plot.axes={ axis(1); axis(2); contour(neap_hu3, add=T )}
)
where neap_hu3 is my raster file.
I've tried adding map(add=TRUE) but that didn't work. also tried plotting a shapefile of the boundary after the filledContour plot but that didn't work.
anyone has any suggestions? will be greatly appreciated :)
I have a 3D plot using RGL. I would like to make identical plots using color to highlight the distribution of some variable. To do this I would like to have identical plots, how do I find and set the orientation of a plot?
Once I make a preliminary plot, I move it around to find a nice display angle and I would like to save that angle and incorporate it into future plotting scripts. Anyone have a suggestion on how to do this?
library(rgl)
plot3d(iris)
#play with the plot to find a good angle
#save the angle for future plots
Ben's comment basically answers your question; this just applies expand.dots to what he wrote ;)
## In an inital session:
library(rgl)
plot3d(iris)
## Now move the image around to an orientation you like
## Save RGL parameters to a list object
pp <- par3d(no.readonly=TRUE)
## Save the list to a text file
dput(pp, file="irisView.R", control = "all")
.......
## Then, in a later session, to recreate the plot just as you had it:
library(rgl)
pp <- dget("irisView.R")
plot3d(iris)
par3d(pp)
I have a data.frame of x/y/z points. I know how to make a 3d scatterplot using the rgl package but I would like to connect each point in the scatterplot to make a wireframe or surface plot.
This code returns the scatter plot
library(rgl)
Data <- expand.grid(x=seq(0,10),y=seq(0,10))
Data$z <- Data$x^2+Data$y^2
plot3d(Data)
While this code returns a blank graph:
plot3d(Data,type='wire')
I can make the plot I want with lattice:
library(lattice)
wireframe(z~x+y,Data)
I can even make it rotate:
library(TeachingDemos)
rotate.wireframe(z~x+y,Data)
But I prefer rgl over lattice because it renders much quicker and lets you rotate the plot with the mouse.
Whats the proper way to make a wireframe plot in rgl?
The surface drawing plot function in rgl is persp3d and like base::persp, it needs a matrix as input to the z argument
zmat <- matrix(Data$z, 11,11)
persp3d(x=seq(0,10), y=seq(0,10), z=zmat)
I did spin this graphic a bit before capturing it with a screen grabbing program I use: