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",...).
Related
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
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.
I'm wondering how to make a map of ONLY a county in R, and if possible, how to do this without using a shapefile.
For example, I'd like to make a map of Los Angeles county in California. Is there anyway for me to call up a map of just LA county?
What I tried is to set xlim and ylim to manually "crop" the map that is displayed (finding the coordinates on Google Maps), but I'm looking for a easier way to make maps of counties.
Here's what I did to plot LA county:
map("county", interior=TRUE, regions="california",
xlim=c(-119.023098 ,-117.435574), ylim=c(33.70319 ,34.941657))
And here's an example of the type of map that I'm looking for:
Suggestions on an easier way to create substantively the same map would be greatly appreciated.
Just use regions with the county name:
map("county", regions="california,los angeles")
I would suggest using ggmap to map the counties.
You could simply get a map of the county using a string:
library(ggmap)
la_county<-get_map('los angeles county')
ggmap(la_county)
Or you could use the x and y coordinates to center the map view:
library(ggmap)
la_county<-get_map(c(-118.2218,34.3582)) #or get_map(c(mean(-119.023098 ,-117.435574), mean(33.70319 ,34.941657)))
ggmap(la_county)
You can also adjust the basemap aesthetics and zoom level:
library(ggmap)
la_county<-get_map('los angeles county', zoom=9, type='toner')
ggmap(la_county)
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.
I'm trying to use R to generate a figure with sample localities for my FieldBio project. So far, I've been able to map the US states I want, to map Canada (national border), to overlay these two maps; what I'd like to do, though, is to map the following states/provinces specifically, excluding the others:
California
Nevada
Utah
Colorado
Wyoming
Montana
Idaho
Washington
Oregon
British Columbia
Alberta
Here is the code I've been using so far:
>map('state', region = c('california', 'nevada', 'utah', 'colorado', 'wyoming', 'montana', 'idaho', 'oregon', 'washington'), xlim=c(-130,-90), ylim=c(30,60), fill=TRUE, col="gray95")
>map("worldHires","Canada", xlim=c(-130,-90), ylim=c(30,60), col="gray95", fill=TRUE, add=TRUE)
This produces a map with the desired states, but canada as a country (cut off at the delimitors I set).
Is there a way to do only the provinces I want? I think I know how to plot the points (I have them as a .csv with lat and long data) on top of this afterward.
I realize that (R: creating a map of selected Canadian provinces and U.S. states) is pretty similar, but I'm getting a little lost in the code for that particular example, and I don't need to highlight anything specific.
Thanks
Like this?
library(raster)
states <- c('California', 'Nevada', 'Utah', 'Colorado', 'Wyoming', 'Montana', 'Idaho', 'Oregon', 'Washington')
provinces <- c("British Columbia", "Alberta")
us <- getData("GADM",country="USA",level=1)
canada <- getData("GADM",country="CAN",level=1)
us.states <- us[us$NAME_1 %in% states,]
ca.provinces <- canada[canada$NAME_1 %in% provinces,]
us.bbox <- bbox(us.states)
ca.bbox <- bbox(ca.provinces)
xlim <- c(min(us.bbox[1,1],ca.bbox[1,1]),max(us.bbox[1,2],ca.bbox[1,2]))
ylim <- c(min(us.bbox[2,1],ca.bbox[2,1]),max(us.bbox[2,2],ca.bbox[2,2]))
plot(us.states, xlim=xlim, ylim=ylim)
plot(ca.provinces, xlim=xlim, ylim=ylim, add=T)
So this uses the getData(...) function in package raster to grab the SpatialPolygonDataFrames for US states and Canadian provinces from the GADM site. Then it extracts only the states you want (notice that the relevant attribute table field is NAME_1 and the the states/provinces need to be capitalized properly). Then we calculate the x and y-limits from the bounding boxes of the two (subsetted) maps. Finally we render the maps. Note the use of add=T in the second call to plot(...).
And here's a ggplot solution.
library(ggplot2)
ggplot(us.states,aes(x=long,y=lat,group=group))+
geom_path()+
geom_path(data=ca.provinces)+
coord_map()
The advantage here is that ggplot manages the layers for you, so you don't have to explicitly calculate xlim and ylim. Also, IMO there is much more flexibility in adding additional layers. The disadvantage is that, with high resolution maps such as these (esp the west coast of Canada), it is much slower.