简体   繁体   中英

R - adding dots to a UK Local Authority choropleth map ggplot

I've made a Choropleth map in R which shades local authorities based on the percentage of households who are in Fuel Poverty.

#converts file into a readable format
options(scipen = 999)

#create a data frame from imported CSV file
mydata <- readr::read_csv("/Users/Desktop/R/Map/Fuel_Poverty.csv")

#import shape file
mymap <-st_read("Local_Authority_Districts__December_2019__Boundaries_UK_BUC.shp", stringsAsFactors = FALSE)

#joins map shape file to local authority data csv
map_and_data <- inner_join(mymap,mydata)

#displays structure of object
str(map_and_data)

#creates a plot
ggplot(map_and_data) +
  geom_sf(aes(fill = Fuel_Poverty_Percentage),lwd = 0.1) +
  theme_void() + 
  scale_fill_gradient2(low = "#a50026", high = "#005abb", midpoint = 170, na.value = "#808080")

I'd now like to layer on some dots which will correspond to specific locations, likely in the form of postcosts or latitude and longitude values. I've tried following a couple of tutorials but seem to just get the dots appearing in the bottom left corner no matter the longitude and latitude values I set?

(sites <- data.frame(lat = c(54.7), long = c(-1.27)))

#creates a plot
ggplot(map_and_data) +
  geom_sf(aes(fill = Fuel_Poverty),lwd = 0.1) +  # lwd = 0.1 is line thickness, 1.5 @5000 pixels to save well
  geom_point(data = sites, aes(x = long, y = lat), size = 3, shape = 21, fill = "red") +
  theme_void() +     #this takes out the background
  scale_fill_gradient2(low = "#005abb", high = "#a50026", midpoint = 12, na.value = "#808080")

Map with point in the wrong location

Since your map data mymap is in the crs (Coordinate Reference System) OSGB 1936 / British National Grid format any point in GPS (latitude, longitude) format has to be translated.

A point in eg London 51.52756, -0.257844 (GPS WGS84 coordinates) translates to 520948.75443717, 182398.33790064 (OSGB36 grid reference using OSTN15 conversion).

  • Using the data in your plot:
mypoint <- data.frame(lat=520948.75443717,long=182398.33790064)

ggplot() +
 geom_sf( data=mymap ) +
 geom_point( data=mypoint, aes(lat, long, size=200) )

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM