Getting error with read_osm from the tmaptools package in R. Want to generate a static map with background layer.
Error is present even when using the example NLD_muni data from tmap package
library(tmap)
library(tmaptools)
library(OpenStreetMap)
tmap_mode("plot")
data(NLD_muni)
test <- tmaptools::read_osm(NLD_muni, type = "esri", zoom = NULL)
Error
Error in FUN(X[[i]], ...) :
Sorry, parameter type `NA' is ambiguous or not supported.
Expected to load a basemap
Could use tmap_mode("view") and created interactive plot but ideally can make a static plot.
I encountered the same problem as you, the examples from the help files don't work anymore for read_osm depending on the help file (this since I upgraded to Windows 10 and reinstalled R - I suspect it is related).
I have found another example on the web which is working for me, and from which you can hopefully start again.
# load Netherlands shape
data(NLD_muni)
# read OSM raster data
osm_NLD <- read_osm(bb(NLD_muni, ext=1.1, projection ="longlat"))
# plot with regular tmap functions
tm_shape(osm_NLD) +
tm_raster() +
tm_shape(NLD_muni) +
tm_polygons("population", convert2density=TRUE, style="kmeans", alpha=.7, palette="Purples")
In order to generate a static base map in tmap using read_osm, the x argument object needs to be in the WGS84 projection (EPSG 4326).
library(tmap)
library(tmaptools)
library(tidyverse)
tmap_mode("plot")
data(NLD_muni)
# This does not work:
NLD_bm <- tmaptools::read_osm(NLD_muni, type = "esri")
# The problem is that NLD_muni is in the projected coordinate system for Netherlands,
# but read_osm wants WGS84
sf::st_crs(NLD_muni) # check the coordinate reference system
# This does work
NLD_bm <- NLD_muni %>%
sf::st_transform(., crs = 4326) %>% # transform NLD_muni to WGS84 projection
sf::st_bbox(.) %>% # extract bounding box of transformed layer
tmaptools::read_osm(., type = "esri") # pass bounding box to read_osm
# Now you can map them
tm_shape(NLD_bm) + tm_rgb() + tm_shape(NLD_muni) + tm_borders(col = "red")
Here is what it looks like:
Two comments:
1) it is not doable to show a basemap with a static {tmap} map; the basemap functionality comes from {leaflet} package, and is therefore available only in view mode.
If you absolutely positively require a basemap in a static map (which is a valid use case) consider using a combination of {ggplot2} and {ggmap}.
2) your loading of NLD_muni is needlesly complex; consider this code instead:
library(tmap)
data(NLD_muni)
tmap_mode("view")
tm_shape(NLD_muni) + tm_polygons(col = "population")
Related
I have two shapefiles, which I would like to plot into a given plot extent. One of the shapefiles exceeds the extent and when plotted it automatically overwrites the limits of the extent. This happens when loading the shapefiles using the terra package and plotting it using the tidyterra functions, but it is not an issue when reading the shapefiles using the old readOGR function and ploting it using the core ggplot2 functions.
# libraries
library(terra)
library(tidyterra)
library(ggplot2)
library(ggspatial)
library(raster)
library(sp)
library(sf)
library(rgdal)
EXAMPLE 1 - I don't want this
# read shapefiles
SHP1 <- terra::vect('file1.shp')
SHP2 <- terra::vect('file2.shp')
# plot
ggplot() +
coord_equal(ylim=c(100000,800000)) +
geom_spatvector(data=SHP1,fill=NA,color='grey',inherit.aes=T) +
geom_spatvector(data=SHP2,fill=NA,color='green',size=1)
EXAMPLE 2 - I want this
# read shapefiles
SHP1 <- terra::vect('file1.shp')
SHP2 <- terra::vect('file2.shp')
ggplot() +
coord_equal(ylim=c(100000,800000)) +
geom_polygon(SHP1,mapping=aes(x=long,y=lat,group=group),fill=NA,color='grey',size=0.1) +
geom_polygon(SHP2,mapping=aes(x=long,y=lat,group=group),fill=NA,color='green',size=0.5)
How could I obtain map from example 2 using the geom_spatvector function used in example 1?
I really would like to use the terra package to read and manipulate shapefiles but then it produces SpatVector class object which is not supperted by the ggplot2 function. This means that only for the plotting purposes I have to transfer it to the older SpatialPolygonsDataFrame and this is exactly what I would like to avoid.
You need to use coord_sf rather than coord_equal. Obviously, we don't have your shapefile, but if I take a similar shapefile of Germany, then we can demonstrate:
library(tidyterra)
library(ggplot2)
p <- ggplot(SHP1) +
geom_spatvector()
Firstly, with the standard geom_spatvector plot:
p
Secondly, with axis limits written by a call to geom_sf:
p + coord_sf(ylim = c(47, 52))
I am trying to create a global cartogram using the cartogram package in R. I am trying to use the data from wrld_simpl. What I expect is a cartogram in which the Population ("Pop2005" variable) is plotted. The code I have developed is this:
data(wrld_simpl)
world<-wrld_simpl
world_sf = st_as_sf(world)
world_sf_proj = st_transform(world_sf, crs = 3785)
world_cartogram <- cartogram_cont(world_sf_proj, "POP2005")
plot(world_cartogram)
Nonetheless, this has resulted in the following figure:
Do you know what is wrong with the code? Maybe the CRS? I have tried to use others CRS but I got the following error:
"Error: Using an unprojected map. This function does not give correct centroids and distances for longitude/latitude data:
Use "st_transform()" to transform coordinates to another projection."
Taken from this documentation, it is stated that
The default plot of an sf object is a multi-plot of all attributes, up
to a reasonable maximum
If you want to use the base R plot function, then use st_geometry(your_map) to plot (the geometry) an sf object.
Another possibility (which I don't recommend) is to set plot options to 1 plot maximum (options(sf_max.plot=1)), but this plots the first variable, and it might not be the best idea.
library(sf)
library(spData)
library(cartogram)
library(tidyverse)
world_sf = st_as_sf(world)
world_sf_proj = st_transform(world_sf, crs = 3785)
world_cartogram <- cartogram_cont(world_sf_proj, "pop")
plot(st_geometry(world_cartogram))
Now, sf is particularly well suited with ggplot2 and the tidyverse. In that setting, just use ggplot in combination with geom_sf.
ggplot(world_cartogram) +
geom_sf()
I'm trying to do a map using library tmap and this dataset.
Following this discussion, I'm using the following simple code:
library(tmap)
library(geobr)
library(sf)
rg = st_as_sf(polygons_regioes_adm_sp)
tm_shape(rg) +
tm_polygons("regiao_administrativa",
legend.show = F) +
tm_text("regiao_administrativa", size = 1/2)
That yields the following map with overlapping DUPLICATE text. I tried insert remove.overlap = TRUE inside tm_text but this doesn't worked better.
Furthermore i'm getting the following message:
old-style crs object detected; please recreate object with a recent sf::st_crs()
In this context, How I get a map without duplicate text ?
Your code to display the map with the tmap library is fine. The problem is that the sf object polygons_regioes_adm_sp has a mixture of POLYGON and MULTIPOLYGON. So, you just need to simplify the sf object by running the st_cast() function.
As for the projection problem, you just need to specify the EPSG code to assign a recent form of the CRS to the sf object (NB: as for the warning message - cf.below - do not worry, it is fine)
So, please find below the code to display correctly the names of the administrative regions.
library(tmap)
library(sf)
library(dplyr) # please, do not forget to load this library
rg <- polygons_regioes_adm_sp %>%
st_set_crs(4674) %>%
st_cast()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
#> that
tm_shape(rg) +
tm_polygons("regiao_administrativa",
legend.show = F) +
tm_text("regiao_administrativa", size = 1/2)
Created on 2021-11-13 by the reprex package (v2.0.1)
I am trying to work with municipality data in Norway, and I'm totally new to QGIS, shapefiles and plotting this in R. I download the municipalities from here:
Administrative enheter kommuner / Administrative units municipalities
Reproducible files are here:
Joanna's github
I have downloaded QGIS, so I can open the GEOJson file there and convert it to a shapefile. I am able to do this, and read the data into R:
library(sf)
test=st_read("C:/municipality_shape.shp")
head(test)
I have on my own given the different municipalities different values/ranks that I call faktor, and I have stored this classification in a dataframe that I call df_new. I wish to merge this "classification" on to my "test" object above, and wish to plot the map with the classification attribute onto the map:
test33=merge(test, df_new[,c("Kommunekode_str","faktor")],
by=c("Kommunekode_str"), all.x=TRUE)
This works, but when I am to plot this with tmap,
library(tmap)
tmap_mode("view")
tm_shape(test33) +
tm_fill(col="faktor", alpha=0.6, n=20, palette=c("wheat3","red3")) +
tm_borders(col="#000000", lwd=0.2)
it throws this error:
Error in object[-omit, , drop = FALSE] : incorrect number of
dimensions
If I just use base plot,
plot(test33)
I get the picture:
You see I get three plots. Does this has something to do with my error above?
I think the main issue here is that the shapes you are trying to plot are too complex so tmap is struggling to load all of this data. ggplot also fails to load the polygons.
You probably don't need so much accuracy in your polygons if you are making a choropleth map so I would suggest first simplifying your polygons. In my experience the best way to do this is using the package rmapshaper:
# keep = 0.02 will keep just 2% of the points in your polygons.
test_33_simple <- rmapshaper::ms_simplify(test33, keep = 0.02)
I can now use your code to produce the following:
tmap_mode("view")
tm_shape(test_33_simple) +
tm_fill(col="faktor", alpha=0.6, n=20, palette=c("wheat3","red3")) +
tm_borders(col="#000000", lwd=0.2)
This produces an interactive map and the colour scheme is not ideal to tell differences between municipalities.
static version
Since you say in the comments that you are not sure if you want an interactive map or a static one, I will give an example with a static map and some example colour schemes.
The below uses the classInt package to set up breaks for your map. A popular break scheme is 'fisher' which uses the fisher-jenks algorithm. Make sure you research the various different options to pick one that suits your scenario:
library(ggplot2)
library(dplyr)
library(sf)
library(classInt)
breaks <- classIntervals(test_33_simple$faktor, n = 6, style = 'fisher')
#label breaks
lab_vec <- vector(length = length(breaks$brks)-1)
rounded_breaks <- round(breaks$brks,2)
lab_vec[1] <- paste0('[', rounded_breaks[1],' - ', rounded_breaks[2],']')
for(i in 2:(length(breaks$brks) - 1)){
lab_vec[i] <- paste0('(',rounded_breaks[i], ' - ', rounded_breaks[i+1], ']')
}
test_33_simple <- test_33_simple %>%
mutate(faktor_class = factor(cut(faktor, breaks$brks, include.lowest = T), labels = lab_vec))
# map
ggplot(test_33_simple) +
geom_sf(aes(fill = faktor_class), size= 0.2) +
scale_fill_viridis_d() +
theme_minimal()
library(sp)
library(spdep)
library(ggplot2)
library(ggmap)
library(rgdal)
Get and fiddle with data:
nc.sids <- readShapePoly(system.file("etc/shapes/sids.shp", package="spdep")[1],ID="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
nc.sids=spTransform(nc.sids,CRS("+init=epsg:4326"))
Get background map from stamen.com, plot, looks nice:
ncmap = get_map(location=as.vector(bbox(nc.sids)),source="stamen",maptype="toner",zoom=7)
ggmap(ncmap)
Create a data frame with long,lat,Z, and plot over the map and a blank plot:
ncP = data.frame(coordinates(nc.sids),runif(nrow(nc.sids)))
colnames(ncP)=c("long","lat","Z")
ggmap(ncmap)+geom_point(aes(x=long,y=lat,col=Z),data=ncP)
ggplot()+geom_point(aes(x=long,y=lat,col=Z),data=ncP)
give it some unique ids called 'id' and fortify (with vitamins and iron?)
nc.sids#data[,1]=1:nrow(nc.sids)
names(nc.sids)[1]="id"
ncFort = fortify(nc.sids)
Now, my map and my limits, I want to plot the 74 birth rate:
myMap = geom_map(aes(fill=BIR74,map_id=id),map=ncFort,data=nc.sids#data)
Limits = expand_limits(x=ncFort$long,y=ncFort$lat)
and on a blank plot I can:
ggplot() + myMap + Limits
but on a ggmap I can't:
ggmap(ncmap) + myMap + Limits
# Error in eval(expr, envir, enclos) : object 'lon' not found
Some versions:
> packageDescription("ggplot2")$Version
[1] "0.9.0"
> packageDescription("ggmap")$Version
[1] "2.0"
I can add geom_polygon to ggplot or ggmap and it works as expected. So something is up with geom_map....
The error message is, I think, the result of an inheritance issue. Typically, it comes about when different data frames are used in subsequent layers.
In ggplot2, every layer inherits default aes mappings set globally in the initial call to ggplot. For instance, ggplot(data = data, aes(x = x, y = y)) sets x and y mappings globally so that all subsequent layers expect to see x and y in whatever data frame has been assigned to them. If x and y are not present, an error message similar to the one you got results. See here for a similar problem and a range of solutions.
In your case, it's not obvious because the first call is to ggmap - you can't see the mappings nor how they are set because ggmap is all nicely wrapped up. Nevertheless, ggmap calls ggplot somewhere, and so default aesthetic mappings must have been set somewhere in the initial call to ggmap. It follows then that ggmap followed by geom_map without taking account of inheritance issues results in the error.
So, Kohske's advice in the earlier post applies - "you need to nullify the lon aes in geom_map when you use a different dataset". Without knowing too much about what has been set or how they've been set, it's probably simplest to globber the lot by adding inherit.aes = FALSE to the second layer - the call to geom_map.
Note that you don't get the error message with ggplot() + myMap + Limits because no aesthetics have been set in the ggplot call.
In what follows, I'm using R version 2.15.0, ggplot2 version 0.9.1, and ggmap version 2.1. I use your code almost exactly, except for the addition of inherit.aes = FALSE in the call to geom_map. That one small change allows ggmap and geom_map to be superimposed:
library(sp)
library(spdep)
library(ggplot2)
library(ggmap)
library(rgdal)
#Get and fiddle with data:
nc.sids <- readShapePoly(system.file("etc/shapes/sids.shp", package="spdep")[1],ID="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
nc.sids=spTransform(nc.sids,CRS("+init=epsg:4326"))
#Get background map from stamen.com, plot, looks nice:
ncmap = get_map(location=as.vector(bbox(nc.sids)),source="stamen",maptype="toner",zoom=7)
ggmap(ncmap)
#Create a data frame with long,lat,Z, and plot over the map and a blank plot:
ncP = data.frame(coordinates(nc.sids),runif(nrow(nc.sids)))
colnames(ncP)=c("long","lat","Z")
ggmap(ncmap)+geom_point(aes(x=long,y=lat,col=Z),data=ncP)
ggplot()+geom_point(aes(x=long,y=lat,col=Z),data=ncP)
#give it some unique ids called 'id' and fortify (with vitamins and iron?)
nc.sids#data[,1]=1:nrow(nc.sids)
names(nc.sids)[1]="id"
ncFort = fortify(nc.sids)
#Now, my map and my limits, I want to plot the 74 birth rate:
myMap = geom_map(inherit.aes = FALSE, aes(fill=BIR74,map_id=id), map=ncFort,data=nc.sids#data)
Limits = expand_limits(x=ncFort$long,y=ncFort$lat)
# and on a blank plot I can:
ggplot() + myMap + Limits
# but on a ggmap I cant:
ggmap(ncmap) + myMap + Limits
The result from the last line of code is: