How can I animate using SPPLOT? - r

I have a shapefile with 5000+ polygons.
I have a netCDF file with timeseries data for several variables with one common key with the shapefile.
Static Map
For any particular time, I plot a static map by
Merging netCDF data for one timestep into the shapefile
Plot the shapefile using spplot
Animation
Now, I want to create an animation for a small time period.
For ggplot2, there are packages such as gganimate etc.
But, how can I do this using spplot?
P.S. GGPLOT2 is too slow for this purpose.

Try the animation package?
Remember that spplot is a lattice based function, so if you're doing this in a function you'll need to print() inside the function.

Related

Making interactive plot of time series rasterbrick using R

My goal is to make an interactive plot of rain radar time serie, I want to is display the raster layer I want through a scroll bar in the plot area, or with HTML output like dygraph can do.
For now, I use the raster, levelplot and animate packages to make a gif of the event (with sp to add shapefile), but the result is not very good for my use of it, and not pretty.
Is that possible and how?
I tried to use the htmlwidget and leaflet without success. They do not allow the multiband raster except for RGB purpose. so when I tried, it gaves me the sum of all the layers in one.
I don't know what script I could give to help you. Ask me if you need something.

Overlapping data contour on a map

I have gone through few tutorials and answers here in stackoverflow such as:
Overlap image plot on a Google Map background in R or
Plotting contours on an irregular grid or Geographical heat map of a custom property in R with ggmap or How to overlay global map on filled contour in R language or https://blog.dominodatalab.com/geographic-visualization-with-rs-ggmaps/
They either don't serve my purpose or consider the density of the data to create the image.
I am looking for a way to plot contour on a map of a certain data, and would expect the image to look something like this:
or something like this taken from https://dsparks.wordpress.com/2012/07/18/mapping-public-opinion-a-tutorial/:
I have a data here that gives a contour plot like this in plot_ly but i want this over the map given by latitudes and longitudes.
Please guide me on how this can be done. Any links to potential answers or codes would be helpful.
Ok I did some digging and figured that to plot the data -which in this case are point values randomly distributed across the Latitude and Longitude, one has to make it continuous instead of the discreetly distributed one. To do this I interpolated the data to fill in the gaps, this method is given in Plotting contours on an irregular grid and then take it from there. Now the interpolation here is done using a linear regression, one can use other methods such as IDW, Kriging, Nearest Neighbourhood etc for which R-packages are easily available. These methods are widely used in climatology and topographic analysis. To read more about interpolation methods see this paper.

Plot points on map R maps package

I have a dataset of all powerplants and I've got their locations down to the format the maps package in R likes c("arkansas,clay", "arkansas,conway", ...).
Some counties have more than one powerplant, and there are 7+ types of powerplants, so I'd like to plot them as points on a map and not just color the counties, as I can see the maps package mainly doing. Was thinking to jitter their position a bit. But I don't know how to go from state/county name to location, or plot straight up points in the maps package.
Does anyone have any suggestions?
So I couldn't figure out how to do it with the maps package, but with ggplot, it's almost trivial. The first few lines of this answer made it really easy to construct a plot I needed.
Plotting bar charts on map using ggplot2?
One trick I did use was to create R's version of a hastable from the map_data in ggplot2.
usaMap=maps_data("county")
usaMap$locCode=paste(usaMap$region,",",usaMap$subregion,sep="")
usaMap2 = usaMap[!duplicated(usaMap$locCode),]
row.names(usaMap2)=usaMap2$locCode
currentGen$long = usaMap2[currentGen$locCode,"long"]+rnorm(nrow(currentGen),0,.05)
currentGen$lat = usaMap2[currentGen$locCode,"lat"]+rnorm(nrow(currentGen),0,.05)
where currentGen is my powerplants data frame and the format of the region matches exactly the format of usaMap$locCode.

Custom background image with ggmap

I'm using R and ggmap to analyse some geographic data. I would like to use my own background map (a tif file) instead of the ones provided by ggmap (google maps, osm, stamen, etc.).
Is there a way to load my own raster file and display it using ggmap ? I really want to keep ggmap as most of my analysis use ggmap's functions (geom_density2d, geom_tile, etc.). For example a wrapper that transform a raster file reads by the raster package to a ggmap get_map object ?
Thanks

How to convert from shape into polygon in R? There was shape2poly(shapefiles) but this function have been removed

How to convert from shape into polygon in R? There was shape2poly(shapefiles) but this function have been removed, are shapefiles, maptools, spdep still packages for handling maps in R?
I tend to use the OGR stuff, as it lets me work with data from a range of sources (geodatabases, kml, etc).
library(rgdal)
mylayer <- readOGR(dsn="/path/to/folder/containing/shapefile",
layer="shapefilename-minus-dot-shp")

Resources