Plotting a bar chart on World map using Rworldmap Package - r

I have a data set df, as below
Country Rev2013 Rev2014 Rev2015
China 56.15 2.26 106.90
United States 33.41999 138.12 96.62
United Kingdom 32.93 28.74 101.86
Brazil 20.42 48.17 118.24
Indonesia 92.30 55.95 128.71
Germany 5.02039 11.06 64.39
For the above data set I want to plot a bar chart using rworldmap something similar to this.

Spatial data is quite large topic, but to cut the idea short:
rworldmap has built in global shapemap:
require(rworldmap)
shapeMap <- getMap()
Next step would be understanding that Spatial Polygon object that you just made has similar structure to data frame.
You can add more columns to shapeMap#data where, obviously, all of the data assiciated with polygons is stored.
Such as:
shapeMap#data$Rev2013 <- df$Rev2013[match(shapeMap#data$NAME_SORT, df$Country)]
This is just to give you an idea how to start off with setting up data. Next step is plotting. Now here you have to make a choice - either with spplot or ggplot2.

Related

Is it possible to create a map of the UK counties with the packages spData/tmap

I have recently used the package spData for plotting the London boroughs but I haven't found anything for plotting the counties in the UK.
Is it possible the the package spData contains only a few countries in it?
Thank you for your help,
Andrea
Could this be what you are looking for? A shp of the counties?
spData isn't a complete set of world spatial data.
You can find a complete set (with good county shapes) in the Natural Earth 10m (high resolution) cultural vectors. Here's how I was able to make a quick map of UK counties, coloured by the length of the county name:
library("tmap")
library("tmaptools")
library("rnaturalearth")
library("rnaturalearthhires")
library("sf")
data("states10")
uk_counties <- states10[which(states10$iso_a2 =="GB"),]
uk_counties = st_as_sf(uk_counties)
qtm(uk_counties, fill="name_len")
Once you have this dataframe called uk_counties, you can join it to your data & quickly make some nice maps.
choropleth of uk counties by length of county name

Creating maps in R: Missing islands?

I am trying to map a harbour (Conception Bay South) in Newfoundland, Canada but, the main island (Bell island) is missing. I've been trying for days to figure out how to fix it.
When I add world.cities the city that is on that island shows up, but the island itself is missing.
I know that maps has a river layer and a lake layer that can be added as needed, but is there an island layer I'm just not finding? Or do I have to get the data some other way? If so, how would I do that?
I would prefer to not have to plot this in ggplot or ggmap, because for everything except the missing islands 'maps' is working perfectly for me. But I'll obviously do that if there isn't another solution.
Here is my code:
require(maps)
require(mapdata)
map('worldHires','Canada',
xlim=c(-53.5, -52.500),
ylim=c(47.2,48.25),
col="grey75", fill=TRUE)
map.axes(cex.axis=0.8)
map.scale(-53.41,47.29,
ratio=FALSE,
relwidth=0.1,
cex=0.5)
Use the raster package to get level 2 admin data for Canada:
require(raster)
cdn = getData("GADM",country="CAN",level=2)
plot(cdn, xlim=c(-53.5, -52.500), ylim=c(47.2,48.25))
shows a small island in the bay and some more on the end of the peninsula.
This is a base R graphics plot, so use base plotting functions to put things on top, change the colour, axes etc.
Yes, the islands appear to be missing. A solution would be to use ggmap with dplyr.
library(dplyr)
library(ggmap)
get_googlemap(center = "Conception Bay South", zoom = 10) %>% ggmap()
I should also point out, that if you have a .shp file, you can use that instead of ggmap.

R how to add border countries to a country spatialpolygons map

I am trying to make some colerful maps using spatialpolygons with R. I downloaded the shapefile from gadm.org website and colored the regions as I wanted following http://bl.ocks.org/prabhasp/raw/5030005/ . However, I think the map would look much better if I were able to put on the map also part of the countries Vietnam shares the border with, as in this wikipedia map
I have no clue what to start with, could someone give me a hint? Should I dowload the regional map and then join the regions for the border countries and work at the provincial level for Vietnam? Or can I plot my Vietnam map over the country level one?
If this is a one-off, I'd be inclined to do it this way.
library(raster)
library(ggplot2)
vietnam <- getData("GADM",country="Vietnam",level=2)
china <- getData("GADM",country="China",level=0)
laos <- getData("GADM",country="Laos",level=0)
cambodia <- getData("GADM",country="Cambodia",level=0)
thailand <- getData("GADM",country="Thailand",level=0)
ggplot(vietnam,aes(x=long,y=lat,group=group))+
geom_polygon(aes(fill=id),color="grey30")+
geom_polygon(data=china,fill="grey60",color="grey80")+
geom_polygon(data=laos,fill="grey60",color="grey80")+
geom_polygon(data=cambodia,fill="grey60",color="grey80")+
geom_polygon(data=thailand,fill="grey60",color="grey80")+
coord_map(xlim=c(-1,1)+bbox(vietnam)["x",],ylim=c(-1,1)+bbox(vietnam)["y",])+
scale_fill_discrete(guide="none")+
theme_bw()+theme(panel.grid=element_blank())
Labelling the border countries is trickier because you have to know where to put the labels, and you can't use the country centroids because they are off the map. I'd be includes to eyeball it, and use annotate(geom="text",...).

Overlay shapefile and rasterlayer in R?

I have a raster layer with climate data from the north of Mexico, part of Canada and the US mainland. I am trying to restrict the climate data to only the zone of the US mainland. To do this I thought it would be easy to import a US mainland map and overlay it with my raster data (of course this is turning out to be a lot more difficult than I thought). So far, using the rgdal library, I have managed to import a shapefile including the USA mainland map with states divisions. Now I want to convert this into a raster layer so that can finally overlay it with my initial climate raster layer. This is the code that I am using:
setwd ("C:/Climate_data/USA map")
ogrInfo(".", "USA_mainland_states")
usa_state = readOGR(dsn=".", layer="USA_mainland_states")
##Convert to Raster
r_usa_state <- raster()
extent(r_usa_state) <- extent(usa_state)
rasterize(usa_state,r_usa_state, fun='last')
overlay (r_usa_state, sms_av, fun='mask')
However, now I get the following error:
Error in .readCells(x, cells, 1) : no data on disk or in memory
sms_av is the climate rasterlayer (103936 elements, 823.3 kb).
Also, when I do:
hasValues(r_usa_state)
I get:
FALSE
What am I doing wrong?? Any advice would be GREATLY appreciated!!!!

How do I add information from a data frame onto a US Map in R?

I have a data frame based on the global terrorism database called gtd in R.
I have the maps package downloaded/required
What I am trying to do is take data from my gtd data frame and add the points on a US map.
I know the code for the USA map is map("state")
in my gtd data frame, there is a column for latitude and longitude
all I want to do is plot the points on the US map to show location of where terrorist attacks happened in US
so from my gtd database to only get a subset where American longitude/latitude is needed I know I would use
subset(x=gtd, country_txt=="United States")
but from there, how to I plot the location of attacks as points on the USA map?
Much appreciated!
I'm going to make the broad assumption from clues in your post that you're using data from the global terrorism database. If you've managed to read in the huge CSV from it, what you want to do should work and you're really close to having the answer. Basically, you just need to add the points() to the map():
library(maps)
# assuming you have it in a CSV file
dat <- read.csv("gtd.csv")
# subset only points in the US
US <- dat[dat$country_txt == "United States",]
# plot the base US map
map('state')
# add points with really horrid default color, point type & size
# changing this is a good exercise for the poster :-)
points(x=US$longitude, y=US$latitude, col="red", cex=0.75)
This has nothing to do with ggplot2 though, so consider removing that tag. It can be done with ggplot but maps() doesn't use it.

Resources