How to use regions' names as labels in spatial Data? - r

I use the following code to visualize some SpatialPolygonsDataFrame with ggplot2:
require(shapefiles)
require(sp)
xx <- readShapeSpatial(system.file("shapes/sids.shp", package="maptools")[1],
IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
xx.ff <- fortify(xx,region="NAME")
x <- ggplot(xx.ff) +
aes(long,lat, fill = id) +
geom_polygon()
to end up with:
Assume that I am using fill to represent something else, like regional theft rate. Thus I want to label those regions with their names. is there a way to write the regions name right onto the map into the corresponding region, e.g. I want to write Halifax right onto the region.

Have a look at similar/related questions I asked:
ggplot centered names on a map
Improve centering county names ggplot & maps
Plot county names on faceted state map (ggplot2)

Related

Heat Map in ggplot(), getting all the layers plotted

I am making a heat map of flooding incidents in the UK. I am following the example listed here. However, I am using a different base map from the example and it won't show up on the map. For my base map I use shapefiles provided by the UK gov, found here, and named it uk.shp, an sf object. Flooding data is proprietary and I cannot share but the original format is polygon shapefile. I then turn those shapefiles into gridded points so I can plot a continuous heat map, this sf object is named pt.shp.
Here is the the base map, original shapefile, and gridded points overlaid for context. You can see there are many floods here, often laying under the same point. I constructed a grid sf object that repeats a given point and uses flood ID as the unique identifier. Below is an example of the data with proprietary information removed. One possible issue I can think of is st_intersection returned the lat and long of the flooding shapefile (which I set as the mapping aes()) but the point's mapping coords are listed in the geometry column of the data.
However, when I use stat_density2d() with my base map, the continuous plot disappears. Below is my plotting code w/ each mapping iteration.
# base map plots
base <- ggplot()+
geom_sf(data=uk.shp)
# Plot density of the points
ggplot()+
stat_density2d(data=pt.shp, aes(x=long, y=lat, fill = ..density..), geom='tile', contour = F)
# base map shows up w/o density map? But legend exists so it is being mapped...
base +
stat_density2d(data=pt.shp, aes(x=long, y=lat, fill = ..density..), geom='tile', contour = F, alpha = .5) +
viridis::scale_fill_viridis(option='inferno')
Some issues are clear, like the plotting window of the heatmap has different dimensions than the base map. However, my main issues is I can't overlay the two plots and I don't know why.

R - ggplot with facets: placing facets on the map

I visualize some data using ggplot2 package with facets. Data can be, e.g., distribution of some values over different continents (or cities, countries etc). Here are some toy data and a standard solution for making a primary plot with facet_grid() function.
library(ggplot2) # key library
library(reshape2) # to convert to long format
databas<-read.csv(data=
"continent,apples,bananas
North America,30,20
South America,15,34.5
Europe,15,19
Africa,5,35")
databaslong<-melt(databas) # default conversion, will make first col.as id
# plotting as colored bars
ggplot(databaslong,aes(x=variable,y=value,fill=variable))+geom_col()+facet_grid(.~continent)
Following is predictably produced:
But now I would like to have more control on the positions of facets. Particularly, it seems natural to put each on the world map onto respective positions on continents, that is some action typical for tmap or similar packages. For example, use native ggplot instrumentality:
mapWorld <- borders("world", colour="gray50", fill="gray50")
ggplot() + mapWorld
and to get like such (manually combined these two layers in inkscape):
Is it possible to achieve such a combination of ggplot and mapping packages in a programmable way?

maps r plot distances with color on these

perhaps you have an idea and could help me. I have following data:
lon.x <- c(11.581981, 13.404954, 9.993682, 7.842104 , 11.741185)
lat.x <- c(48.135125, 52.520007, 53.551085, 47.999008, 48.402880)
lon.y <- c(8.801694, 7.842104 , 11.581981, 13.404954, 7.842104 )
lat.y <- c(53.079296,47.999008, 48.135125, 52.520007, 47.999008)
pred <- c(1,2,3,4,5)
data <- data.frame(cbind(lon.x, lat.x, lon.y, lat.y, pred))
where "lon.x" and "lat.x" are longitude-latitude points of a city and "lon.y" and "lat.y" of another city. So there are pairs of cities.
Now, I would like to make a map in R, with
(1) the direct distances between the x and y coordinates as a line
(2) which will receive a different color based on the variable "pred", this could be red for higher values and blue for lower, or thicker lines with higher values of "pred".
The result should be a simple map, with lines between the cities, that are shaped based on the variable "pred". For instance, the line between the first pair of cities would be thinner, while the last one would be thicker. Is that possible?
I have currently only made to receive a (very complicated) google map of Germany:
library(mapproj)
map <- get_map(location = 'Germany', zoom = 6.2)
ggmap(map)
But I am not sure how to plot points and especially relations between the points that differ based on "pred". Also a very simple map (not so detailed google map) would be best! Any idea? THANKS!
You can use ggplot2 to add lines onto the plot.
library(ggplot2)
library(ggmap)
map <- get_map(location = 'Germany', zoom = 6)
ggmap(map) +
geom_segment(data=data, aes(x=lon.x, xend=lon.y, y=lat.x, yend=lat.y, color=pred), size=2) +
scale_color_continuous(high="red", low="blue")
As for the simpler map, you can download shape files (just the outlines of countries) from www.gadm.org. Level 0 maps are just the country, level 1 have state boundaries, etc. To use one of these, download the file from the website and use this code:
load("DEU_adm0.RData")
gadm <- fortify(gadm)
ggplot(gadm) +
geom_path(aes(x=long, y=lat, group=group)) +
geom_segment(data=data, aes(x=lon.x, xend=lon.y, y=lat.x, yend=lat.y, color=pred), size=2) +
scale_color_continuous(high="red", low="blue")

Rstudio: plot lat/long to US map, from data file, with time series, issues

Update to the update on color: I have Postal_Code in the data file. So I would need the help in banding the Postal_Code and mark the dot/bubble with color accordingly to the number of times a lat/long dot/bubble is plotted and is also in the high or low banded range of a postal_code.
For example, the banding range of Postal_Code to color:
1 - 20 White
21 - 40 Blue
41 - 60 Green
61 - 80 Yellow
81 - 100 Purple
101 - 120 Orange
120 - beyond Red
--
Update: Progress: I have plotted teh lat/long to a US map. Here is the string of code I used to accomplish:
# After data extraction and convert to .csv, load file to Rstudio
map <- get_map(location = 'united states',
zoom = 4, source = 'stamen') ggmap(map, fullpage = TRUE);
# call libs for maps and plotting already installed
library(ggplot2)
library(maps)
#load us map data
all_states <- map_data("state")
#plot all states with ggplot
p <- ggplot()
p <- p + geom_polygon( data=all_states,
aes(x=long, y=lat, group = group),colour="white", fill="grey10" )
p;
# plot data points (lat/long) to US map
p + geom_point(data=latlonguvdec14,
aes(y=Latitude, x=Longitude), color="red");
I have successfully accomplished the main task, get the lat/long plotted to the US map. Now I need help in making this a time series or a time lapse type of display, sounds kinda like animation. And I'm open to ideas on how to display.
I want the oldest lat/long data to display first, and then populate that way till the end, say from 12/01/2014 to 12/31/2014.
Then I want to use color and size of dot/bubble to change, as the data is being plotted over time to the map. Say if the same dot/bubble get hit twice, change to a color, if again then another color, and up-tick it in size a little each time. Some sort of variation on this would be very helpful.
Imagine these are phone calls coming into a call center, some will be repeat callers.
--
I have installed Rstudio 0.98.1103. I have installed the plug-ins "ggmap" and "ggplot2". I have created a .csv file that has a header and has lat/long data, and date/time, the file layout looks like this:
start_dt, start_tm, latitude, longitude
12/31/2014, 150432, 41.75, -83.63
And I have been able to display a Google Map, via this code:
library(ggplot2)
library(maps)
#load us map data
all_states <- map_data("state")
#plot all states with ggplot
p <- ggplot()
p <- p + geom_polygon( data=all_states, aes(x=long, y=lat, group = group),colour="white", fill="grey10" )
p
The issue: I'm trying to plot the data points of the lat/long data to the US map, and can't seem to get any plot command to work. I've been all over Google/Youtube trying to find examples and not everyone demos ggplot2. Once I get the lat/long data points to plot with color and size, etc, then I want to be able to run this as a time series, I think that is the correct verbiage..., where it reads the date or date and time, and then plots in oldest to newest order, each lat/long data point.
Any and all help for this newbie to R is greatly appreciated.
Thank You

Spatial Plot in R : how to plot the polygon and color as per the data to be visualized

I have been trying to draw the county based Choropleth map in R for visualizing my dataset for the State of Arizona.
For plotting the thematic map using the polygon bases data for the county from arizona.edu (Spatial Library) and data is from az.gov
It have the following for plotting the COUNTY polygon-
library(maptools)
library(rgdal)
library(ggplot2)
library(plyr)
county <- readShapePoly(file.choose())
county#data$id <- rownames(county#data)
county.points <- fortify(county, coords="id")
county.df <- join(county.points, county#data, by="id")
ggplot(county.df) + aes(long,lat,group=group, fill="id") +
geom_polygon() +
geom_path(color="white") +
coord_equal() +
scale_fill_brewer("County Arizona")
This code is not giving me any error and also no output.
My Source of Shape file here
Data Source here
I can't speak to why your code is not generating output - there are too many possible reasons - but is this what you are trying to achieve?
Code
library(rgdal)
library(ggplot2)
library(plyr)
library(RColorBrewer)
setwd("< directory with all your files >")
map <- readOGR(dsn=".",layer="ALRIS_tigcounty")
marriages <- read.csv("marriages.2012.csv",header=T,skip=3)
marriages <- marriages[2:16,]
marriages$County <- tolower(gsub(" ","",marriages$County))
marriages$Total <- as.numeric(as.character(marriages$Total))
data <- data.frame(id=rownames(map#data), NAME=map#data$NAME, stringsAsFactors=F)
data <- merge(data,marriages,by.x="NAME",by.y="County",all.x=T)
map.df <- fortify(map)
map.df <- join(map.df,data, by="id")
ggplot(map.df, aes(x=long, y=lat, group=group))+
geom_polygon(aes(fill=Total))+
geom_path(colour="grey50")+
scale_fill_gradientn("2012 Marriages",
colours=rev(brewer.pal(8,"Spectral")),
trans="log",
breaks=c(100,300,1000,3000,10000))+
theme(axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank())+
coord_fixed()
Explanation
To generate a choropleth map, ultimately we need to associate polygons with your datum of interest (total marriages by county). This is a three step process: first we associate polygon ID with county name:
data <- data.frame(id=rownames(map#data), NAME=map#data$NAME, stringsAsFactors=F)
Then we associate county name with total marriages:
data <- merge(data,marriages,by.x="NAME",by.y="County",all.x=T)
Then we associate the result with the polygon coordinate data:
map.df <- join(map.df,data, by="id")
Your specific case has a lot of potential traps:
The link you provided was to a pdf - utterly useless. But poking around a bit revealed an Excel file with the same data. Even this file needs cleaning: the data has "," separators, which need to be turned off, and some of the cells have footnotes, which have to be removed. Finally, we have to save as a csv file.
Since we are matching on county name, the names have to match! In the shapefile attributes table, the county names are all lower case, and spaces have been removed (e.g., "Santa Cruz" is "santacruz". So we need to lowercase the county names and remove spaces:
marriages$County <- tolower(gsub(" ","",marriages$County))
The totals column comes in as a factor, which has to be converted to numeric:
marriages$Total <- as.numeric(as.character(marriages$Total))
Your actual data is highly skewed: maricopa county had 23,600 marriages, greenlee had 50. So using a linear color scale is not very informative. Consequently, we use a logarithmic scale:
scale_fill_gradientn("2012 Marriages",
colours=rev(brewer.pal(8,"Spectral")),
trans="log",
breaks=c(100,300,1000,3000,10000))+

Resources