Plot multiple shapefile over multiple raster - r

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

Related

Is there a way to single out data points in a jammed ggplot?

This is a ggplot produced by fviz_cluster function. The problem is that I want to single out some data points or even a single one and label it by name. I can easily identify the data points that are on the edges, but what happens for those in the middle of the pack. I thought of approaching this by labelling every data point and then zoom in, but the plot is not interactive and I can't find a way to do that. Any idea on how to approach this would be much appreciated
Here is my code:
r=as.data.frame(colnames(df))
remov=c(1,2,5,7,11,14,16,21,23,24,25,26,89,29:54)
df=df[,-remov]
rem=c(2,3,6,7,8,9,12,13,14,15,16)
rownam=as.data.frame(unique(df$Name))
k=df[,-rem]
k= k[!duplicated(df$Name),]
k <- data.frame(k[,-1], row.names = k[,1])
k=na.omit(k)
kme=kmeans(k,centers = 6,nstart = 25)
str(kme)
fviz_cluster(kme, data = k,geom="point")
If you want an easy workaround to make your graph interactive use the ggplotly() function.
install plotly:
install.packages("plotly")
library(plotly)
plot <- fviz_cluster(kme, data = k,geom="point")
plot %>% ggplotly()
# or
ggplotly(plot)

Overlay a shapfile on a (raster) filledContour plot

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.

Plotting nb object in ggplot?

I would like to plot the outline of UK, along with an nb object that I generated from a spatial points dataframe.
The problem is that the outline of UK takes really, really long to plot --it keeps crashing my Rstudio. This for example, either take really long to load or my Rstudio just stops responding.
library(raster)
UK_gadm <- getData("GADM", country="GB", level=0)
plot(UK_gadm)
So I resorted to using ggplot2 from this solution where I can get the outline of UK like in a fraction of a second with the following commands:
library(ggplot2)
UK <- map_data(map = "world", region = "UK") # changed map to "world"
ggplot(data = UK, aes(x = long, y = lat, group = group)) +
geom_polygon() +
coord_map()
The issue now is that I would like the nb object to be plotting against the backdrop of the outline of UK -- however, this seems only achievable in base R like for example:
plot(orotl.shp, xlim=c(-125, -115), ylim=c(42,47))
plot(orstationc.neighbors.dist, orstationc.loc, add=T, lwd=2, col="blue")
Is there any way I could plot nb objects in ggplot or is there a way for R to plot the outline of UK without crashing my computer with the base R plot function?
Managed to find a fast, simple solution after a whole night of effort. Hope this helps someone else with a similar issue.
Just to elaborate on the goal: plot a neighbours object (nb) against a shapefile. This is to visualise the linkages among certain coordinates. After some googling, I think this can only be done with base R's plot function. The problem however, was loading a country's shapefile (downloaded from official sources/gadm)-- its too big.
To solve this issue, get a more generalised, simple map of the country via the maps package, turn it into a shapefile and then plot it alongside the neighbours object.
Here's the code:
library(spdep)
# get your neighbour object from your spatial points df
rest_neighbours <- dnearneigh(rest_spdf,0,1)
library(maps)
# get boundary of UK
UK_map <- sf::st_as_sf(maps::map(database='world',regions='UK', plot = FALSE, fill = TRUE))
# write to shapefile
st_write(UK_map, 'shapefiles/UK.shp')
# henceforth, we can just call the shapefile
UK <- readOGR('shapefiles/UK.shp')
# plot the boundary and the neighbours
plot(UK)
plot(rest_neighbours, rest_coords, add=T, lwd=2, col="blue")
I did not realise that official boundary files are often really detailed which also means that they are really huge and I'm glad that there's ready-made watered down versions of the maps available in the maps package of r. (Sorry if you already knew -- I'm still learning!)
Hope this helps anyone else!

adding land boundary to a filled.contour plot

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

Save the orientation of a RGL plot3d() plot

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)

Resources