Я пытаюсь отобразить данные петиции правительства Великобритании в R. Я использовал граничные данные с географического портала ONS. Код работает, и первая карта, которую я создал, также работает.
#Install packages
install.packages("tidyverse")
install.packages("jsonlite")
install.packages("geojsonio")
install.packages("sp")
install.packages("parlitools")
install.packages("rvest")
install.packages("xml2")
install.packages("magrittr")
#Load packages
library(tidyverse)
library(jsonlite)
library(geojsonio)
library(sp)
library(parlitools)
library(rvest)
library(xml2)
library(magrittr)
[#GETTING PETITION DATA
#Importing petition for UK-wide lockdown from JSON format
petition <- fromJSON("https://petition.parliament.uk/petitions/301397.json", flatten = TRUE)
signatures <- petition$data$attributes$signatures_by_constituency %>%
rename(constituency = name)
#MAPPING BOUNDARIES
#Save url for boundary data UK
url <- "https://opendata.arcgis.com/datasets/b64677a2afc3466f80d3d683b71c3468_0.geojson"
#Load and save the boundary data as uk_map
uk_map <- geojson_read (url, what = "sp")
#pcon18cd is code name for constituency (as we can see when we view uk_map). Use fortify to get this data.
fort_uk_map <- fortify(uk_map, region = "pcon18cd")
#MAPPING PETITION DATA
#Join map data to signatures data from constituency using left_join
full_uk_map <- left_join(fort_uk_map, signatures, by = c("id" = "ons_code"))
#Plot-1a: Map of signatures in the whole of UK
ggplot() +
geom_polygon(data = full_uk_map, aes(x = long, y= lat, group = group, fill = signature_count)) +
geom_path(color = "black", size = 0.1) +theme(legend.position = "bottom") +
theme_void() +
labs(x = NULL,
y = NULL,
title = "Signatories of the UK Coronavirus Lockdown Petition",
subtitle = "Let's investigate where the signatures come from",
caption = "Geometries: ONS Open Geography Portal; Data: UK Parliament and Government",
fill = "Signature Count")][1]
Но, как видно из изображения, более высокие подписи имеют более светлый цвет. Я хотел бы изменить это так, чтобы большее количество подписей имело более темный цвет.
Итак, я попробовал этот код чуть ниже кода выше, и вот где я сталкиваюсь с проблемами.
#Change color of legend so that higher signature count equals darker color. Use quantile () [Doesn't work]
no_of_classes <- 9
quantiles <- quantile(full_uk_map$signature_count, probs = seq(0, 1, length.out = no_of_classes + 1))
labels <- c()
for(band in 1:length(quantiles)){
labels <- c(labels, paste0(round(quantiles[band])," - ", round(quantiles[band + 1])))
}
full_uk_map$quantiles <- cut(full_uk_map$signature_count, breaks = quantiles, labels = labels,
include.lowest = T)
labels <- labels[1:length(labels)-1]
#Plot-1b: Map of signatures in the whole of UK [Doesn't work]
sig_map_by_quantile <- ggplot() +
geom_polygon(data = full_uk_map, aes(x = long, y = lat, group = group, fill = quantiles)) +
geom_path(color = "black", size = 0.1) +
scale_fill_brewer(type = 'qual', palette = "Blues", guide = "legend", name = "Signature Count", labels = labels) +
theme_void +
theme(legend.position = "bottom") +
labs(x = NULL,
y = NULL,
title = "Signatories of the UK Coronavirus Lockdown Petition",
caption = "Geometries: ONS Open Geography Portal; Data: UK Parliament and Government")
Когда я запускаю full_uk_map $ quantiles, я вижу следующее сообщение об ошибке:
> full_uk_map$quantiles <- cut(full_uk_map$signature_count, breaks = quantiles, labels = labels,
+ include.lowest = T)
Error in cut.default(full_uk_map$signature_count, breaks = quantiles, :
lengths of 'breaks' and 'labels' differ
Кто-нибудь сможет помочь? Очень ценится!