I downloaded a polygon shape file (england_ct_1991.shp) from a zip file I downloaded here and CSV file (england_ct_1991.csv) from a zip file here that I want to connect together (all public data). I added a new column to the CSV file called 'price' so each county has a unique price like this:
name,label,x,y,price
Avon,9,360567.1834,171823.554,11
Avon,9,322865.922,160665.4829,11
Bedfordshire,10,506219.5005,242767.306,20
Berkshire,11,464403.02,172809.5331,23....
I joined the shp and CSV by the county name. The problem is the map is not superimposing on the price to show a nice color gradient on the counties based on the price. I checked some YouTube tutorials stating the important part is joining but it worked for them so I am unsure what I did wrong?
library(ggplot2)
library(sf)
library(tidyverse)
# map of england counties
map7 <- read_sf("england_ct_1991.shp")
head(map7)
ggplot(map7) +
geom_sf()
# get x (longitude) y (latitude) county names and prices
totalPrices <- read_csv("england_ct_1991.csv")
head(totalPrices)
# join map and csv data on county name
mappedData <- left_join(map7, totalCounts, by="name")
head(mappedData)
# print map
map1 <- ggplot(mappedData, aes( x=x, y=y, group=name)) +
geom_polygon(aes(fill=price), color="black") +
geom_sf()
map1
The key point is that the warning that
Detected an unexpected many-to-many relationship between x and ywhen runningleft_join(map7, totalCounts, by="name").So keep your
totalPricesdata to be unique, that is, no duplicated regions innamecolumn.