-
Notifications
You must be signed in to change notification settings - Fork 125
Description
I am currently writing a .Rmd (R markdown document) on downloading and visualising the Local Authority Districts (LADs) and transport regions of the UK within Rstudio.
> R.version
_
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 4
minor 2.0
year 2022
month 04
day 22
svn rev 82229
language R
version.string R version 4.2.0 (2022-04-22)
I am doing this in a .Rmd so that this document can be hosted on github as a vignette.
I am having an issue whereby I have four separate plots which should all have unique titles set with qtm(title = 'desired_title')
as per the documentation, however all titles are being assigned to my first plot and none of the plots after, even though the other plots are building correctly (legend, polygon colours etc.)
I have been knitting the document locally to view my changes rather than uploading every edit to GH to visualise my edits (knitr version: 1.39
)
Here are the steps to reproduce my issue:
Note: code inbetween __ lines indicate seperate code chunks within the .Rmd
______________________
library(sf)
library(tmap)
library(dplyr)
______________________
______________________
# Download data from the openinfra repo
url_regions_lad = "https://github.com/udsleeds/openinfra/raw/main/data-small/lads_joined_2021.geojson"
url_transport_regions = "https://github.com/udsleeds/openinfra/raw/main/data-small/regions_new.geojson"
______________________
______________________
# Load data into a Simple Features frame
regions_lad = read_sf(url_regions_lad)
transport_regions = read_sf(url_transport_regions)
______________________
______________________
# Set tmap mode to interactive
tmap::tmap_mode('view') # After this, all tmap::qtm() plots will be interactive
______________________
______________________
# Here we create the interactive plots
sf::sf_use_s2(use_s2 = FALSE)
regions_lad %>% tmap::qtm(title = 'LADs of UK & NI')
______________________
______________________
# Here we create an interactive plot of all LADs, coloured by the region they reside in
tmap_options(max.categories = 78) # Increase max categories so every (78) transport region has its own colour
regions_lad %>% tmap::qtm(fill = 'Region_name', title = 'LADs of UK & NI coloured by region')
______________________
______________________
# Here we plot the transport regions of England
transport_regions %>% tmap::qtm(title = 'Transport regions of England')
______________________
______________________
# Here we create the coloured interactive plot of England's transport regions
transport_regions %>% tmap::qtm(title = 'Coloured transport regions of England', fill = 'Region_name')
______________________
Note that sf::sf_use_s2(use_s2 = FALSE)
is a fix for errors (1 2) due to an upgrade to the sf package
What I expect:
I expect to see each of the four plots with their own respective titles added, much like this
What I get:
Instead I have all four of my plot titles (the most recently created being the top title) added to the first plot and none after.
The figures themselves are building correctly (This one has coloured all LADs based on the LAD region name as expected specifying fill='Region_name'
) with the exception of titles, as can be seen in the example above of coloured LADs, there is no title in the plot despite using
regions_lad %>% tmap::qtm(fill = 'Region_name', title = 'LADs of UK & NI coloured by region')
See the plot title issues on the vignette hosted on GH here
I should also note that running the same code as a .R script (rather than a .Rmd rendered with knitr) included below does have the desired outcome - that is four seperate interactive plots within the 'Viewer' pane of Rstudio each with the correct plot title, I can switch between plots using ()
However as I was looking to uploaded this as a vignette tutorial I would still like this to work as such. I just don't know if this may be an issue with tmap::qtm or the knitr package, or how these two packages interact and was hopeing someone has come across this issue before.
R script
library(sf) # Simple Features (sf) library for storing geospatial data
library(tmap) # tmap library for interactive plots
library(dplyr)
# Download data from the openinfra repo
url_regions_lad = "https://github.com/udsleeds/openinfra/raw/main/data-small/lads_joined_2021.geojson"
# Load data into a Simple Features frame
regions_lad = read_sf(url_regions_lad)
# Download data from the openinfra repo
url_transport_regions = "https://github.com/udsleeds/openinfra/raw/main/data-small/regions_new.geojson"
# Load data into a Simple Features frame
transport_regions = read_sf(url_transport_regions)
# Set tmap mode to interactive
tmap::tmap_mode('view') # After this, all qtm() plots will be interactive
# Here we create the interactive plots
sf::sf_use_s2(use_s2 = FALSE)
regions_lad %>% tmap::qtm(title = 'LADs of UK & NI')
tmap_options(max.categories = 78) # We increase max categories so that every transport region has its own colour
regions_lad %>% tmap::qtm(fill = 'Region_name', title = 'LADs of UK & NI coloured by region')
transport_regions %>% tmap::qtm(title = 'Transport regions of England')
# Here we create the interactive plot of England's transport regions
transport_regions %>% tmap::qtm(title = 'Coloured transport regions of England', fill = 'Region_name')