R animate ggplot of lat/lon coords using date - r

I have a data set with about 180,000 observations. I can plot all of them using lat/lon coords and ggplot. When I plot them all at once, i get big blob. Any tips on how to create an animation, where each frame will plot all of the plots on a given date? Note that there are often many observations per date.
Thanks in advance
my data has the following columns.
Created Date, Latitude, Longitude
3/19/14, 40.62143617 -73.92598905
3/19/14 40.65808826 -73.84443243
3/18/14 40.64067217 -73.95307493
I used ggplot2 to plot all of the coords with the code below.
require(ggplot2)
cold <- read.csv(fn, header=TRUE)
r <- ggplot(cold, aes(x=Longitude, y=Latitude,)) + geom_point(size=.9, alpha=.02)

The tkexamp function in the TeachingDemos package can be used to make an interactive GUI for your own functions and one of the controls it has an animate control.
Just write a function that takes the date (as a number) as and argument and plots the subset of the data based on that date, then pass that function along with a list specifying the animate control to tkexamp. There will be a slider that you can use to move through time, or click the play button next to the slider and it will scroll through for you.
The last 2 examples for tkexamp show using it with ggplot2 and the final example for the USCrimes data set (TeachingDemos package) shows using the animate control for tkexamp. Combine these with your data and you will be able to view your data animated.

Related

R Plot of Two Detrended Series Shows Line Chart Rather Than Scatterplot

I have a set of data which are within the exact same time frame, with the exact same number of points. I have detrended both so comovement can be analyzed. When I plot them against each other the graph attempts to create a line chart including dates.
plot
This is what the series look like in the environment:
environment variables
This is what the data looks like:
data screenshot
I would like this in a scatterplot measuring against both variables, just points and no lines or dates in the plot.
So I sorta figured it out but it's super botched and I do not recommend anyone else to do this.
Essentially, I bound the two datasets together doing:
testvar <- cbind(dewagerealM, dewagerealF)
I was then able to select all the data on the left and the right, then plot them against each other like so:
plot(testvar[1:23,1], testvar[1:23,2])
This seems to have worked but it's not pretty and definitely not what should be done but it seems to have gotten the job done.
The easiest way to do this is to use the options xy.lines and xy.labels set to FALSE
plot(dewagerealM, dewagerealF, type = 'p',xy.lines = FALSE, xy.labels = FALSE)
Since you are plotting time series (ts) type objects, the help function help("plot.ts") can give you more details on the options you can use to plot these objects.

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.

How can I animate using SPPLOT?

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.

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.

Google Maps vs. ggplot2/MarMap

Below is a JavaScript page I have created that allows me add and freely move markers on the map. From this map I can figure out the regions I am interested in.
Basically what I want to do is show the same map using ggplot2/MarMap with coastline indicators + bathymetry data. I am really just interested in getting bathymetry data per GPS location, basically getting negative/positive elevation per Lat+Long, so I was thinking if I can plot it then I should be able to export data to a Database. I am also interested in coastline data, so I want to know how close I am (Lat/Long) to coastline, so with plot data I was also going to augment in DB.
Here is the R script that I am using:
library(marmap);
library(ggplot2);
a_lon1 = -79.89836596313478;
a_lon2 = -79.97179329675288;
a_lat1 = 32.76506070891712;
a_lat2 = 32.803624214389615;
dat <- getNOAA.bathy(a_lon1,a_lon2,a_lat1,a_lat2, keep=FALSE);
autoplot(dat, geom=c("r", "c"), colour="white", size=0.1) + scale_fill_etopo();
Here is the output of above R script:
Questions:
Why do both images not match?
In google-maps I am using zoom value 13. How does that translate in ggplot2/MarMap?
Is it possible to zoom in ggplot2/MarMap into a (Lat/Long)-(Lat/Long) region?
Is it possible to plot what I am asking for?
I don't know how you got this result. When I use your script, I get an error since the area your are trying to fetch from the ETOPO1 database using getNOAA.bathy() is too small. However, adding resolution=1 (this gives the highest possible resolution for the ETOPO1 database), here is what I get:
To answer your questions:
Why do both images not match?
Probably because getNOAA.bathy() returned an error and the object dat you're using has been created before, using another set of coordinates
In google-maps I am using zoom value 13. How does that translate in ggplot2/MarMap?
I have no clue!
Is it possible to zoom in ggplot2/MarMap into a (Lat/Long)-(Lat/Long) region?
I urge you to take a look at section 4 of the marmap-DataAnalysis vignette. This section is dedicated to working with big files. You will find there that you can zoom in any area of a bathy object by using (for instance) the subsetBathy() function that will allow you to click on a map to define the desired area
Is it possible to plot what I am asking for? Yes, but it would be much easier to use base graphics and not ggplot2. Once again, you should read the package vignettes.
Finally, regarding the coastline data, you can use the dist2isobath() function to compute the distance between any gps point and any isobath, including the coastline. Guess where you can learn more about this function and how to use it...

Resources