[R] Cannot create a visible Choropleth Legend - GISTools
Henry Cann
henri.cann at gmail.com
Mon Mar 6 20:54:36 CET 2017
I have combined two layers to create a shapefile which I am colouring using
the Choropleth function in GISTools. The problem I am having is that
despite having no other problems with my code including with the actual
mapping of the choropleth itself, nothing I can do, using choro.legend,
actually appears. I have checked I am plotting in the right pace using
locator().
My code is below. I have also attached my data and link to shapefile and
attached my data.
Many thanks in advance.
Henry
https://borders.ukdataservice.ac.uk/ukborders/easy_download/prebuilt/shape/infuse_dist_lyr_2011.zip
require(rgdal)
require(sp)
library(plyr)
library(dplyr)
library(RColorBrewer)
MyDataFile2 <- read.csv("reduced1.csv")
class(MyDataFile2)
coordinates(MyDataFile2)<-~X+Y
class(MyDataFile2)
writeOGR(MyDataFile2, "Folder", "File9", driver = "ESRI Shapefile")
Redefined2 <- readOGR(dsn="Folder", layer = "File9")
proj4string(Redefined2) <- CRS("+init=epsg:4326") # WGS 84
MyMap <-readOGR(".","infuse_dist_lyr_2011")
proj4string(MyMap)
MyMapb <- spTransform(MyMap, proj4string(Redefined2))
MyMapb at data <- mutate(MyMapb at data, id_poly =
as.numeric(rownames(MyMapb at data)))
Redefined2 at data <- mutate(Redefined2 at data, id_la =
as.numeric(rownames(Redefined2 at data)))
New <- over(Redefined2, MyMapb)
New <- mutate(New, id_la = as.numeric(rownames(New)))
New <- left_join(Redefined2 at data, New, by = c("id_la" = "id_la"))
New2 <- New %>% group_by(id_poly) %>%
dplyr::summarise(avgBrit = mean(Brtshns), SumBrit = sum(Brtshns),
nBrit = n()) %>%
arrange(id_poly)
MyMapb at data <- left_join(MyMapb at data, New2, by = c("id_poly" = "id_poly"))
View(MyMapb)
shape2 <- MyMapb[!is.na(MyMapb at data$avgBrit),]
myshading = auto.shading(shape2$avgBrit, n=7,
cols=brewer.pal(7, "Spectral"))
choropleth(shape2, shape2$avgBrit,shading=myshading)
choro.legend(48.4,-5.79, myshading,fmt="%4.1f",cex=0.8)
More information about the R-help
mailing list