Raster Merging in R - r

I need a little help with some R syntax to complete what (I think) is a fairly straightforward task- hopefully someone can assist!
I have a raster map of the UK which is split into postcode areas (e.g. DE, NG, NR etc. 127 postcodes in total).
I have installed the package 'raster' and have successfully plotted the .img in R. All working and looks correct with the raster.
I also have a comma delimited CSV file containing the same postcodes as the raster with another column next to it containing revenue for each postcode.
I was wondering if someone could help me merge/bind the revenue figures into the correct postcode in the raster so that I can plot revenue per postcode.
I feel I should be using cbind and reclassify to do this but I can't work it out on my own
Any help would be appreciated. Thanks in advance!
This is the code I have so far...not rocket science just yet.
setwd("C:\\Users\\[username]\\Documents\\GIS\\Test Data")
require(raster)
revenue<-read.table("revenue.csv",header=T,row.names=1,sep=",")
postcodes<-raster("C:\\Users\\[username]\\Documents\\GIS\\Test Data\\rasters\\postcodes\\postcodes.img")
trim(postcodes)
plot(postcodes)

You should be able to do this with the 'subs' method. You do not show us much about your data (e.g. head(revenue)), but it should work like this:
library(raster)
setwd("C:\\Users\\[username]\\Documents\\GIS\\Test Data")
postcodes <- raster("rasters\\postcodes\\postcodes.img")
revenue <- read.csv("revenue.csv")
subs(postcodes, revenue, by='code', which='rev')
where 'code' and 'rev' would be the column names in data.frame revenue that identify the postcode and revenue fields.

Related

Need to merge yield data for each GDMID with shape file and plot the map filled with yield

I am trying to plot an Indian district shapefile filled with modeled yield, corresponding to each GDMID, common in shapefile and .rds dataframe. I need to first merge the Yield data with respect to each GDMID from the .RDS file with the district shapefile of India. Each district is represented by a GDMID. After merging, the Indian district map filled with modeled yield values needs to be plotted. Shape file has five features, and one of them is "GDMID".
I feel there is something wrong with my code for merging and plotting by ggplot.
I have provided my files in the given link. link of code, shape file and .RDS data frame
I am new to the spatial mapping with R so needs help clarity with steps.
Thanks in advance,
Ranjeet

Extracting values from inside polygons raster r

I'm trying to find the mean daily temperature for counties in South Dakota from raster grids ('bil' files) found at http://prism.oregonstate.edu/. I am getting county boundaries from the 'maps' package.
library(maps)
library(raster)
sd_counties <- map('county','south dakota')
sd_raster <- raster('file_path')
How do I extract the grid cells within each county? I think I need to turn each county into it's own polygon to do this, but how? Then, I should be able to do something like the following. Any help would be greatly appreciated.
values <- extract(raster, list of polygons)
polygon_means <- unlist(lapply(values, FUN=mean))
I'm not familiar with the maps package or the map function, but it looks like it's solely for visualization, rather than geospatial operations.
While there might be a way to convert the map object to actual polygons, here's an easy way sing raster's getData function that works:
library(raster)
usa_adm2 <- getData(country='USA',level=2)
sd_counties <- usa_adm2[grepl('South Dakota',usa_adm2$NAME_1),]
plot(sd_counties)
Now you can extract pixels for each county using extract(r,sd_counties), where r is your desired raster.
Note, that depending on the number of pixels (and layers) you need to extract, that can take some time.

Flow mapping in R

I'm trying to plot trips between zipcodes in R. Specifically, I'm trying to create an interactive where you can click on each zipcode, and see the other zipcodes colored according to how many people traveled from the zip you clicked on to the other zipcodes. Sort of like this: https://www.forbes.com/special-report/2011/migration.html
But less fancy; just showing "out-migration" would be super.
I've been messing with this in R using the leaflet package, but I haven't managed to figure it out. Could someone with better R skills help me out? Any insight would be much appreciated.
I've downloaded a shapefile of zipcodes in LA county from here:
https://data.lacounty.gov/Geospatial/ZIP-Codes/65v5-jw9f
Then I used the code below to create some toy data.
You can find the zipcode shapefiles here:
https://drive.google.com/file/d/0B2a3BZ6nzEGJNk55dmdrdVI2MTQ/view?usp=sharing
And you can find the toy data here:
https://drive.google.com/open?id=0B2a3BZ6nzEGJR29EOFdjR1NPR3c
Here's the code I've got so far:
require(rgdal)
setwd("~/Downloads/ZIP Codes")
# Read SHAPEFILE.shp from the current working directory (".")
shape <- readOGR(dsn = ".", layer = "geo_export_89ff0f09-a580-4844-988a-c4808d510398")
plot(shape) #Should look like zip codes in LA county
#get a unique list of zipcodes
zips <- as.numeric(as.character(unique(shape#data$zipcode)))
#create a dataframe with all the possible combination of origin and destination zipcodes
zips.df <- data.frame(expand.grid(as.character(zips),as.character(zips)), rpois(96721,10))
#give the dataframe some helpful variable names
names(zips.df) <- c("origin_zip", "destination_zip","number_of_trips")
Like I said, any help would be much appreciated. Thanks!

twitteR search geocode argument in R

I want to run a simple search using twitteR but only return tweets located in the U.S. I know twitteR has a geocode argument for lat/long and miles within that lat/long, but this way of locating tweets for an entire country seems hard.
What would I input into the argument to only get US tweets?
Thanks,
I did a brief search around and it looks like twitteR does not have a built-in country argument. But since you have lat/long, it's very straightforward to do a spatial join to a US country shapefile (i.e. point in polygon).
In this example, I'm using the shapefile from Census.gov and the package spatialEco for its point.in.polygon() function. It's a very fast spatial-join function compared to what other packages offer, even if you have hundreds of thousands of coordinates and dozens of polygons. If you have millions of tweets -- or if you decide later on to join to multiple polygons, e.g. all world countries -- then it could be a lot slower. But for most purposes, it's very fast.
(Also, I don't have a Twitter API set up, so I'm going to use an example data frame with tweet_ids and lat/long.)
library(maptools) # to
library(spatialEco)
# First, use setwd() to set working directory to the folder called cb_2015_us_nation_20m
us <- readShapePoly(fn = "cb_2015_us_nation_20m")
# Alternatively, you can use file.choose() and choose the .shp file like so:
us <- readShapePoly(file.choose())
# Create data frame with sample tweets
# Btw, tweet_id 1 is St. Louis, 2 is Toronto, 3 is ouston
tweets <- data.frame(tweet_id = c(1, 2, 3),
latitude = c(38.610543, 43.653226, 29.760427),
longitude = c(-90.337189, -79.383184, -95.369803))
# Use point.in.poly to keep only tweets that are in the US
coordinates(tweets) <- ~longitude+latitude
tweets_in_us <- point.in.poly(tweets, us)
tweets_in_us <- as.data.frame(tweets_in_us)
Now, if you look at tweets_in_us you should see only the tweets whose lat/long fall within the area of the US.

Choropleth world map

I have read so many threads and articles and I keep getting errors. I am trying to make a choropleth? map of the world using data I have from the global terrorism database. I want to color countries on a factor of nkills or just the number of attacks in that country.. I don't care at this point. Because there are so many countries with data, it is unreasonable to make any plots to show this data.
Help is strongly appreciated and if I did not ask this correctly I sincerely apologize, I am learning the rules of this website as I go.
my code (so far..)
library(maps)
library(ggplot2)
map("world")
world<- map_data("world")
gtd<- data.frame(gtd)
names(gtd)<- tolower(names(gtd))
gtd$country_txt<- tolower(rownames(gtd))
demo<- merge(world, gts, sort=FALSE, by="country_txt")
In the gtd data frame, the name for the countries column is "country_txt" so I thought I would use that but I get error in fix.by(by.x, x) : 'by' must specify a uniquely valid column
If that were to work, I would plot as I have seen on a few websites..
I have honestly been working on this for so long and I have read so many codes/other similar questions/websites/r handbooks etc.. I will accept that I am incompetent when it comes to R gladly for some help.
Something like this? This is a solution using rgdal and ggplot. I long ago gave up on using base R for this type of thing.
library(rgdal) # for readOGR(...)
library(RColorBrewer) # for brewer.pal(...)
library(ggplot2)
setwd(" < directory with all files >")
gtd <- read.csv("globalterrorismdb_1213dist.csv")
gtd.recent <- gtd[gtd$iyear>2009,]
gtd.recent <- aggregate(nkill~country_txt,gtd.recent,sum)
world <- readOGR(dsn=".",
layer="world_country_admin_boundary_shapefile_with_fips_codes")
countries <- world#data
countries <- cbind(id=rownames(countries),countries)
countries <- merge(countries,gtd.recent,
by.x="CNTRY_NAME", by.y="country_txt", all.x=T)
map.df <- fortify(world)
map.df <- merge(map.df,countries, by="id")
ggplot(map.df, aes(x=long,y=lat,group=group)) +
geom_polygon(aes(fill=nkill))+
geom_path(colour="grey50")+
scale_fill_gradientn(name="Deaths",
colours=rev(brewer.pal(9,"Spectral")),
na.value="white")+
coord_fixed()+labs(x="",y="")
There are several versions of the Global Terrorism Database. I used the full dataset available here, and then subsetted for year > 2009. So this map shows total deaths due to terrorism, by country, from 2010-01-01 to 2013-01-01 (the last data available from this source). The files are available as MS Excel download, which I converted to csv for import into R.
The world map is available as a shapefile from the GeoCommons website.
The tricky part of making choropleth maps is associating your data with the correct polygons (countries). This is generally a four step process:
Find a field in the shapefile attributes table that maps (no pun intended) to a corresponding field in your data. In this case, it appears that the field "CNTRY_NAME" in the shapefile maps to the field "country_txt" in gtd database.
Create an association between ploygon IDs (stored in the row names of the attribute table), and the CNTRY_NAME field.
Merge the result with your data using CNTRY_NAME and country_txt.
Merge the result of that with the data frame created using the fortify(map) - this associates ploygons with deaths (nkill).
Building on the nice work by #jlhoward. You could instead use rworldmap that already has a world map in R and has functions to aid joining data to the map. The default map is deliberately low resolution to create a 'cleaner' look. The map can be customised (see rworldmap documentation) but here is a start :
library(rworldmap)
#3 lines from #jlhoward
gtd <- read.csv("globalterrorismdb_1213dist.csv")
gtd.recent <- gtd[gtd$iyear>2009,]
gtd.recent <- aggregate(nkill~country_txt,gtd.recent,sum)
#join data to a map
gtdMap <- joinCountryData2Map( gtd.recent,
nameJoinColumn="country_txt",
joinCode="NAME" )
mapDevice('x11') #create a world shaped window
#plot the map
mapCountryData( gtdMap,
nameColumnToPlot='nkill',
catMethod='fixedWidth',
numCats=100 )
Following a comment from #hk47, you can also add the points to the map sized by the number of casualties.
deaths <- subset(x=gtd, nkill >0)
mapBubbles(deaths,
nameX='longitude',
nameY='latitude',
nameZSize='nkill',
nameZColour='black',
fill=FALSE,
addLegend=FALSE,
add=TRUE)

Resources