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.
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'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 used the below data and the code to color the countries using rworldmap.
ddf = read.table(text="country value
USA 10
UK 30
Sweden 50
Japan 70
China 90
Germany 100", header=T)
library(rworldmap)
spdf <- joinCountryData2Map(ddf, joinCode="NAME",
nameJoinColumn="country")
mapCountryData(spdf, nameColumnToPlot="value", catMethod="fixedWidth")
The map is looking fine. The problem I am having is to reduce the gap between the map and the title of the map. Also at the bottom I am having a huge space at the end of the map. I would like to reduce both the spaces.
#jbaums is correct, the shape of the plotting device influences how much white space there is around the plot.
There is a function named mapDevice() in rworldmap that has default values that work well for plotting world maps.
Using RStudio in windows I put this before your code :
mapDevice("x11")
to create the plot below. (x11 is a windows graphics device, away from windows mapDevice() may work )
An alternative option is to reduce any margin whitespace by putting this before your code:
par(mar=c(0,0,1,0)) #(bottom, left, top, right)
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",...).
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.