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 :)
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 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.
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 have to draw maps using R package so that country limits are plotted, and also the contours for the values of a meteorological variable read from a NetCDF file.
I do the following:
r=raster('netcdffile.nc')
map('worldHires', xlim=c(-10,50), ylim =c(30,50))
plot(r, add = TRUE)
contour(r, add = TRUE)
but the country limits don't appear.
It seems that the plotting of the raster, eliminates the country limits previously drawn.
I need a simple wway, please.
Attaching an example netcdf file with correct coordinates would help. I don't have any netCDF file here on hand to test. Did you try the excellent rasterVis package? You can easily plot using trellis or ggplot, and add the map in the usual ways.
For example with rasterVis and ggplot2 something like this should work:
r=raster('netcdffile.nc')
library(rasterVis)
library(maps)
world <- data.frame(map(plot=FALSE)[c("x","y")])
gplot(r) +
geom_tile(aes(fill=value)) +
geom_path(data=world, aes(x,y)) +
stat_contour(aes(z=value)) +
coord_equal()
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)