1
votes

I'm currently plotting a heatmap of tax incomes by local authority within the UK. However, I want to focus on one specific local authority, for example Edinburgh.

I want to find the max and min coordinates for Edinburgh so I can narrow it down and have a zoomed in view of Edinburgh on its own without seeing the rest of UK map. Online, for the map found on Open GeoPortal Stats, when looking at edinburgh it only gives one value for longitude and latitude but I need two values for each.

I am currently using xlim and ylim within R for this.

ggplot (data = map_data, aes(x=long, y=lat, group=group))+
  geom_polygon(aes(fill=map_data$group))+
  coord_equal(xlim=-3.27826, ylim=55.91119))+
  theme(legend.position = "none")

How would I find the other max and min coordinates within the dataframe required for Edinburgh? As there 5mill observations, or is there a better way to do it than using xlim and ylim?

Any help would be great.

1
What are the other variables in map_data? You probably could avoid using xlim and ylim altogether by filtering out your data to only include points from Edinburgh, which would result in a map of only Edinburgh. - krfurlong
or could you at least hint as to which data set you were using. You told us the site, but it's not very straight forward to find the right file. - tjebo
the link is here geoportal.statistics.gov.uk/datasets/…, under the data column in tab 34 - josh

1 Answers

2
votes

Okay - so if I'm understanding the dataset you're using, I think you have a two options:

1) Filter your data to only show Edinburgh

I think you should be able to filter out all other entries using the lad19nm column.

library(dplyr)
library(ggplot2)
edinburgh.map.data <- map_data %>%
   filter(lad19nm == "City of Edinburgh")

ggplot (data = edinburgh.map.data, aes(x=long, y=lat, group=group))+
  geom_polygon(aes(fill=group)) +
  theme(legend.position = "none")

2) Pull out Edinburgh's coordinates to zoom in

So a slightly more nuanced approach if you want to show the regions around Edinburgh after you zoom in would be to do some data crunching before mapping.

library(dplyr)
library(ggplot2)
edinburgh.limits <- map_data %>%
   filter(lad19nm == "City of Edinburgh") %>%
   summarise(
      min.lat = min(lat, na.rm=T),
      max.lat = max(lat, na.rm=T),
      min.long= min(long, na.rm=T),
      max.long = max(long, na.rm=T),      
   )

ggplot(data = map_data, aes(x=long, y=lat, group=group))+
  geom_polygon(aes(fill=group))+
  coord_equal(xlim=c(edinburgh.limits$min.long,edinburgh.limits$max.long),
              ylim=c(edinburgh.limits$min.lat, edinburgh.limits$max.lat))+
  theme(legend.position = "none")