UK choropleth map challenge - postcodes in 3-4 digit format

I think I am nearly there but my map is still blank:

ggplot() +
  geom_sf(data = uk, alpha = 0.3) +
  geom_sf(data = uk, aes(fill = Score)) +
  scale_fill_gradient(low = "red", high = "green") +
  labs(title = "Scores by region",
       fill = "Scores") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_blank(),
        plot.background = element_blank(),
        legend.position = c(0.10, 0.20),
        plot.margin = margin(5, 0, 5, 0))

What am I doing wrong?

I cannot see anything even if I use just my data:

uk <- right_join(uk, region.scores, by = c('name'='NAME_3'))

Can you help please?

Guessing

ggplot() +
  geom_sf(data = uk, aes(fill = Score), alpha=0.3)

I'm thinking you have not created uk correctly, but I don't know what has gone wrong.

If I run, not worrying about the strange geom_sf(data = uk, alpha = 0.3) bit for now,...

ggplot() +
  geom_sf(data = uk, alpha = 0.3) +
  geom_sf(data = uk, aes(fill = Score)) +
  scale_fill_gradient(low = "red", high = "green") +
  labs(title = "Scores by region",
       fill = "Scores") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_blank(),
        plot.background = element_blank(),
        legend.position = c(0.10, 0.20),
        plot.margin = margin(5, 0, 5, 0))

...with my uk, I get

slavek.pdf (3.5 MB)

and

rs_sf <- st_as_sf(region.scores, coords=c('Cor2','Cor1'), crs=st_crs(uk))
ggshop <- ggplot()+
    geom_sf(data=uk,mapping=aes(fill=Score),colour=NA) +
    geom_sf(data=rs_sf,mapping=aes(shape=Shop))+
    coord_sf(xlim=c(-4,-2),ylim=c(52.6,55.15))

gives ggshop (I had some recollection of wanting to mark shops too) which looks like:
ron.pdf (2.7 MB)

Try typing:

uk %>% filter(!is.na(Score))

Do you get:

Simple feature collection with 18 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -3.542267 ymin: 52.7322 xmax: -1.917635 ymax: 55.04052
Geodetic CRS:  WGS 84
# A tibble: 18 x 6
   name                                         geometry Score Shop   Cor1  Cor2
 * <chr>                              <MULTIPOLYGON [°]> <dbl> <chr> <dbl> <dbl>
 1 BB1   (((-2.486325 53.75063, -2.48614 53.75076, -2.4… 0.761 AAA    53.8 -2.46
 2 BB7   (((-2.301575 53.83923, -2.302783 53.83317, -2.… 0.718 AAA    53.9 -2.39
 3 CA15  (((-3.434122 54.81423, -3.422636 54.81174, -3.… 0.727 BBB    54.7 -3.48
 4 CA3   (((-2.941258 54.89384, -2.942828 54.89457, -2.… 0.778 BBB    54.9 -2.94
 5 CW1   (((-2.457456 53.09846, -2.458429 53.09932, -2.… 0.807 CCC    53.1 -2.43
 6 CW9   (((-2.475873 53.28786, -2.472321 53.28598, -2.… 0.868 CCC    53.3 -2.50
 7 DG12  (((-3.423603 54.97768, -3.421576 54.97954, -3.… 0.765 BBB    55.0 -3.25
 8 PR2   (((-2.67412 53.75663, -2.669994 53.7594, -2.66… 0.733 DDD    53.8 -2.71
 9 PR4   (((-2.922551 53.73032, -2.922743 53.73276, -2.… 0.820 DDD    53.8 -2.83
10 PR5   (((-2.606077 53.72835, -2.609401 53.72796, -2.… 0.438 AAA    53.7 -2.66
11 PR7   (((-2.609039 53.60193, -2.615128 53.60036, -2.… 0.741 DDD    53.6 -2.65
12 ST16  (((-2.131142 52.79716, -2.13177 52.79777, -2.1… 0.845 EEE    52.8 -2.12
13 ST17  (((-2.011258 52.79679, -2.007782 52.79411, -2.… 0.812 EEE    52.8 -2.10
14 ST18  (((-2.011258 52.79679, -2.018583 52.79923, -2.… 0.761 EEE    52.8 -2.08
15 ST3   (((-2.083924 52.964, -2.086534 52.96492, -2.08… 0.68  FFF    53.0 -2.12
16 ST4   (((-2.213804 52.98037, -2.214973 52.98198, -2.… 0.855 FFF    53.0 -2.18
17 ST7   (((-2.19621 53.09024, -2.190033 53.08659, -2.1… 0.831 CCC    53.1 -2.26
18 TF9   (((-2.542488 52.79722, -2.548335 52.79771, -2.… 0.857 FFF    52.9 -2.47

Ron.

Still empty :frowning:

ggplot() +
  geom_sf(data = uk, aes(fill = Score), alpha=0.3) +
  scale_fill_gradient(low = "red", high = "green") +
  labs(title = "Scores by region",
       fill = "Scores") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_blank(),
        plot.background = element_blank(),
        legend.position = c(0.10, 0.20),
        plot.margin = margin(5, 0, 5, 0))

Am I missing anything?

can you run the geom_sf example and see an output ?

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
ggplot(nc) +
  geom_sf(aes(fill = AREA))

seems fine, therefore as Ron suggests investigate your 'uk' object. it must lack something that 'nc' contains for example.

Ron spells out in detail what to type, and what to expect.

Ok, I think we are getting there but my problem is with the uk file.
Nevertheless, I don't know what the problem is. Did ron ran this code?
My entire code looks like this:

library(tidyverse)
library(sf)

#Download UK postcode polygon Shapefile
download.file(
  "http://www.opendoorlogistics.com/wp-content/uploads/Data/UK-postcode-boundaries-Jan-2015.zip",
  "postal_shapefile"
)
unzip("postal_shapefile")

uk <- read_sf('Distribution/Sectors.shp')

region.scores <- data.frame(stringsAsFactors = FALSE,
                            NAME_3 = c("BB7","BB1","PR5","CA3",
                                       "DG12","CA15","CW1","ST7","CW9","PR4","PR2","PR7",
                                       "ST17","ST18","ST16","ST3","ST4","TF9"),
                            Score = c(0.717647058823529,
                                      0.761194029850746,0.4375,0.777777777777778,0.764705882352941,
                                      0.727272727272727,0.807017543859649,0.830769230769231,
                                      0.868421052631579,0.819672131147541,0.732673267326733,
                                      0.741379310344828,0.811764705882353,0.761194029850746,
                                      0.844827586206897,0.68,0.855263157894737,0.857142857142857),
                            Shop = c("AAA","AAA","AAA","BBB",
                                     "BBB","BBB","CCC","CCC","CCC","DDD","DDD","DDD",
                                     "EEE","EEE","EEE","FFF","FFF","FFF"),
                            Cor1 = c(53.874,53.756,53.731,54.907,
                                     54.99,54.712,53.103,53.088,53.259,53.754,53.778,
                                     53.645,52.789,52.814,52.813,52.981,52.995,52.897),
                            Cor2 = c(-2.386,-2.462,-2.656,-2.939,
                                     -3.251,-3.481,-2.434,-2.265,-2.501,-2.833,-2.708,
                                     -2.652,-2.099,-2.081,-2.118,-2.122,-2.183,-2.469))

uk <- left_join(uk, region.scores, by = c('name'='NAME_3'))
uk

uk %>% filter(!is.na(Score))


nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
ggplot(nc) +
  geom_sf(aes(fill = AREA))


ggplot() +
  geom_sf(data = uk, alpha = 0.3) +
  geom_sf(data = uk, aes(fill = Score)) +
  scale_fill_gradient(low = "red", high = "green") +
  labs(title = "Scores by region",
       fill = "Scores") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_blank(),
        plot.background = element_blank(),
        legend.position = c(0.10, 0.20),
        plot.margin = margin(5, 0, 5, 0))

rs_sf <- st_as_sf(region.scores, coords=c('Cor2','Cor1'), crs=st_crs(uk))
ggshop <- ggplot()+
    geom_sf(data=uk,mapping=aes(fill=Score),colour=NA) +
    geom_sf(data=rs_sf,mapping=aes(shape=Shop))+
    coord_sf(xlim=c(-4,-2),ylim=c(52.6,55.15))

And ron.pdf is exactly what I need!
It's just one step from the final solution.
Ron,
Can you please simply share the entire code you have used to create two pdfs?

You have the code used to create the uk map/data sf object and ggshop.

I saved both graphs from active windows using ggsave. Normally in code I'd have done:

pdf(file = 'ron.pdf', height = 4, width =3)
 print(ggshop)
dev.off()

Although the height and width might need playing with to get the size you want (I'd resized the window before using ggsave, and it picks it up automatically).

But I have a question: you are still getting no output right?

Is this because you are loading

Distribution/Sectors.shp

rather than

Distribution/Districts.shp

and are therefore unable to join it to region.scores because it's name field has character strings with 3/4 characters, followed by a space and a number (eg 'AB10 1') and you're trying to match it to strings with the format 'AB10'?

Have you checked what is in the uk object as previously suggested? If I use the Sectors.shp rather than District.shp in my code to setup the uk object, then type uk %>% filter(!is.na(Score)) gives:

Simple feature collection with 0 features and 5 fields
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS 84
# A tibble: 0 x 6
# … with 6 variables: name <chr>, geometry <GEOMETRY [°]>, Score <dbl>,
#   Shop <chr>, Cor1 <dbl>, Cor2 <dbl>

That is, no lines with values for Score (but the join ran OK because it was a left_join so keeps all the rows from uk). I believe this is your problem.

Ron.

The firs map is working!!!
Hurray!

Unfortunately, this one:

rs_sf <- st_as_sf(region.scores, coords=c('Cor2','Cor1'), crs=st_crs(uk))
ggshop <- ggplot()+
  geom_sf(data=uk,mapping=aes(fill=Score),colour=NA) +
  geom_sf(data=rs_sf,mapping=aes(shape=Shop))+
  coord_sf(xlim=c(-4,-2),ylim=c(52.6,55.15))

does not generate anything...

Because it was saved to an object? try print(ggshop)? Or just ggshop ?

1 Like

Of course!

Thank you very much!
I hope that this solution will be useful for others!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.