I am trying to make a Tmap with two points but I am having some issues.
`library(tmap)
library(maps)
library(sf)
library(USAboundaries)
#Open soil pit location data
data<-read.csv("SoilPit_Locations___.csv")
#Converting locations to an sf object
points<-st_as_sf(data,coords=c("Longitude","Latitude"),crs=4326)
#Set up map mode as view for an interactive map
tmap_mode("plot")
#Set up style as natural to view topography
tmap_style("natural")
#Map us borders and add points for locations as dots
#tm_shape(us) + tm_borders("black", lwd = .5) +
m=tm_shape(points)+tm_dots()
m
tmap_save(m, "my_mapp.png")`
The issue I am having is that when I try run my code the two points are on the corners of the map, I would like to plot them on the New Mexico state map so they can look nicer.
I have tried extracting the NM state boundary from the world map but I have been unsuccessful.
If you do:
library(USAboundariesData)
then you get states_contemporary_lores (amongst other things) which are the USA state boundaries, so you can subset NM like this:
nm = states_contemporary_lores[
states_contemporary_lores$state_abbr=="NM",
]
and then nm is an sf object which is just New Mexico. You can map this in tmap using tm_shape(nm) + tm_borders() and then adding whatever other layers you have.
I have downloaded shapefile containing polygons for urban areas. However, I am interested in polygons representing rural areas. Therefore I would like to convert the shapefile containing urban areas polygons to rural areas using R. Thus an inverse of it. My assumption is that areas can either be rural or urban.
I am using the "King County with Natural Shoreline for Puget Sound and Lake Washington" shapefile from http://www5.kingcounty.gov/gisdataportal/. The outline for Lake Washington appears when I plot the shapefile but is not saved as an additional polygon relative to the outline of King County. I wish to color Lake Washington a different color to the rest of the county but am not sure how to do this because the shapefile only has 1 feature. I am trying to manipulate the shapefile in R.
You can extract the "hole" that is Lake Washington and then add that to the plot. This uses base graphics but it's just as easy with ggplot.
library(rgdal)
# your shapefile
kc <- readOGR("kingsh/kingsh.shp", "kingsh")
# extract the singular hole that is Lake Washington and
# make it into something plottable
lw <- SpatialPolygons(list(Polygons(list(kc#polygons[[1]]#Polygons[[2]]), ID="lw")))
plot(kc, col="steelblue")
plot(lw, col="maroon", add=TRUE)
You can find the hole by manually scanning the objects or something like:
unlist(lapply(kc#polygons, function(p) {
polys <- slot(p, "Polygons")
lapply(polys, function(q) {
slot(q, "hole")
})
}))
I had to guess at how you're reading it in since you didn't provide any code.
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'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.