Adding postcode label to a map only where there are survey respondents

Hi there,

I am trying to get postcodes to appear as a label on a map but only when there has been one or more survey respondents from a specific postcode.

I am able to add all the postcodes and not have them all bunch up on each other using the code below, like this...

However, I would like to see how the plot looks when only the postcodes where one or more survey respondents came from (this is the 'unique_postcode' variable) are displayed.

library(sf)
library(tidyverse)
install.packages("cowplot")
library(cowplot)
library(writexl)
library(reprex)

data.frame(
   stringsAsFactors = FALSE,
           postcode = c("4000", "4001", "4002", "4003", "4004"),
         Population = c(93931L, 43756L, 21878L, 10939L, 6419L),
                   SA2.name = c("Brisbane City, Spring Hill","Brisbane City",
                                "Brisbane City","Brisbane City","Spring Hill"),
  Electorate.Rating = c("Inner Metropolitan", NA, NA, NA, NA),
    unique_postcode = c(7L, NA, NA, NA, NA),
                                                   geometry = c("list(list(c(153.025239666, 153.025473126, 153.026247906, 153.027519016, 153.028376036, 153.028749396, 153.028756006, 153.028790936, 153.028848136, 153.028954016, 153.029078186, 153.028655046, 153.028781046, 153.028663036, 153.028707026, 153.028703046, 153.028105026, 153.028053056, 153.029043046, 153.028821886, 153.028704866, 153.030285935, 153.030644915, 153.030308095, 153.030225785, 153.029937185, 153.029618205, 153.029078925, 153.029370995, 153.029944075, 153.030649985, 153.030488035, 153.030454025, 
153.030439015, 153.030453655, 153.030477165, 153.030453035, 153.030360145, 153.030847965, 153.031198035, 153.031312025, 153.031661035, 153.031880034, 153.032052024, 153.032146124, 153.032508094, 153.032980954, 153.033293024, 153.033705034, 153.034049054, 153.034147014, 153.035077514, 153.035099924, 153.035279174, 153.035389004, 153.035468204, 153.035544894, 153.035567194, 153.035508724, 153.035534454, 153.035550044, 153.035302024, 153.034442044, 153.034457044, 153.033981054, 153.033917034, 153.033820044, 
153.033289044, 153.032576024, 153.032408044, 153.032040014, 153.031768024, 153.031351334, 153.031452044, 153.031411054, 153.031252024, 153.031362034, 153.031118024, 153.031002034, 153.031233044, 153.031475033, 153.031537043, 153.031779043, 153.031879013, 153.032473013, 153.032697013, 153.031798023, 153.031722033, 153.031716043, 153.031684033, 153.031653043, 153.031545043, 153.031794042, 153.031791052, 153.031541022, 153.031427032, 153.031291032, 153.031226032, 153.031245042, 153.030885042, 153.030763022, 
153.030747042, 153.030555052, 153.030463032, 153.030410032, 153.030423032, 153.030392042, 153.030326052, 153.029906042, 153.029752052, 153.029540023, 153.029302043, 153.029084023, 153.028824043, 153.028506053, 153.028448013, 153.028173023, 153.027602023, 153.027481023, 153.027327023, 153.027178033, 153.027069043, 153.026164023, 153.025866043, 153.025619043, 153.025340043, 153.024668044, 153.023871024, 153.023623044, 153.022654044, 153.022290024, 153.022117014, 153.022175014, 153.021646024, 153.021467024, 
153.020718045, 153.020559055, 153.020364035, 153.020199015, 153.020033055, 153.019735025, 153.019309035, 153.019116025, 153.019025035, 153.018914035, 153.018770045, 153.018713025, 153.018484495, 153.018325035, 153.018115045, 153.017957045, 153.017864035, 153.017763045, 153.017638035, 153.017516045, 153.017367015, 153.017221015, 153.017057025, 153.016885035, 153.016688045, 153.016503025, 153.016402035, 153.016069955, 153.015346056, 153.015034966, 153.014939186, 153.014880896, 153.014790886, 153.014740116, 
153.014682156, 153.014624226, 153.014565866, 153.014528086, 153.014463996, 153.014314966, 153.014187136, 153.014147936, 153.014084206, 153.013994206, 153.013949196, 153.013917186, 153.013872146, 153.013800896, 153.013737166, 153.013673076, 153.013588106, 153.013553206, 153.013459946, 153.013403086, 153.013351946, 153.013260146, 153.013207956, 153.013146046, 153.013107146, 153.013065056, 153.013008156, 153.012927156, 153.012868106, 153.012795026, 153.012783896, 153.012440066, 153.012333156, 153.012284216, 
153.012238146, 153.012190956, 153.012125076, 153.012061016, 153.012010976, 153.011965966, 153.011924906, 153.011789896, 153.011742036, 153.011594036, 153.011546036, 153.011260026, 153.011203046, 153.010961046, 153.010679056, 153.010628026, 153.010532026, 153.010509036, 153.010393036, 153.010389026, 153.010365046, 153.010524036, 153.010608136, 153.010679056, 153.010741026, 153.010835056, 153.010960026, 153.011088037, 153.011170047, 153.011232027, 153.011317037, 153.011335027, 153.011447047, 153.011481027, 
153.011486027, 153.011549027, 153.011636047, 153.011671047, 153.011876037, 153.011883037, 153.011954037, 153.011963047, 153.011990027, 153.012033017, 153.012104047, 153.012184017, 153.012287027, 153.012505037, 153.013357017, 153.013853097, 153.014609077, 153.014760157, 153.014860057, 153.014979047, 153.014978017, 153.015022297, 153.015056277, 153.015130297, 153.015243267, 153.015452267, 153.015818287, 153.016098277, 153.016370277, 153.016543037, 153.016631257, 153.016994247, 153.017414247, 153.018400956, 
153.020443026, 153.020947196, 153.021757166, 153.022516146, 153.023081136, 153.023673166, 153.024496126, 153.024881096, 153.025239666, -27.452593275, -27.4523413049999, -27.4518694449999, -27.4515164649999, -27.451288265, -27.4511540849999, -27.451261475, -27.4515014249999, -27.4518574749999, -27.452225475, -27.452594475, -27.452820435, -27.453296445, -27.4533604549999, -27.4534244449999, -27.4534674349999, -27.454090445, -27.4541414449999, -27.4549094349999, -27.455141485, -27.4554894149999, -27.4566996249999, 
-27.456974405, -27.457344255, -27.457430845, -27.4577344649999, -27.458057475, -27.458609435, -27.458839005, -27.4592872549999, -27.459820485, -27.4599954349999, -27.460061445, -27.460150455, -27.460578025, -27.4612634449999, -27.461439405, -27.4617134649999, -27.4617964349999, -27.461803446, -27.461805446, -27.461880446, -27.461926456, -27.4619514459999, -27.4619674259999, -27.4620182159999, -27.462087406, -27.462139446, -27.4621844559999, -27.461858446, -27.4617584559999, -27.4607814459999, -27.460801486, 
-27.460939456, -27.4610364759999, -27.4611394459999, -27.461279466, -27.4613264559999, -27.461400736, -27.462340576, -27.4626964359999, -27.4626954459999, -27.462846436, -27.462626456, -27.4628114559999, -27.462846436, -27.462881436, -27.4633464359999, -27.4639804559999, -27.464128456, -27.464782446, -27.465293456, -27.465695126, -27.465792456, -27.465932446, -27.4667014459999, -27.4667254459999, -27.467888446, -27.468424446, -27.4696724359999, -27.470593446, -27.470584456, -27.472070446, -27.472055446, 
-27.475227456, -27.476464456, -27.476597456, -27.4771924459999, -27.477255436, -27.477341446, -27.4774454559999, -27.4776634359999, -27.4779394459999, -27.477993446, -27.4784124459999, -27.4785224459999, -27.478597446, -27.4787044559999, -27.4787904459999, -27.479102446, -27.479070436, -27.479074456, -27.479228446, -27.479273436, -27.4793174559999, -27.479388456, -27.4794464559999, -27.4794734459999, -27.479434446, -27.4794694459999, -27.4794934449999, -27.479489445, -27.479462455, -27.4793894549999, 
-27.479254435, -27.479234445, -27.479487445, -27.478749445, -27.478723455, -27.478657445, -27.478539445, -27.4784124449999, -27.4772264549999, -27.4768894549999, -27.476556455, -27.476208455, -27.4752664449999, -27.474124445, -27.474264455, -27.4729414449999, -27.4724504549999, -27.472209455, -27.471858455, -27.4713254549999, -27.471160445, -27.470479445, -27.4703604549999, -27.470300434, -27.470209434, -27.470084454, -27.4698574339999, -27.469498444, -27.469404444, -27.469356434, -27.469282434, 
-27.469156454, -27.469100434, -27.4688512939999, -27.4686774539999, -27.4684444439999, -27.4682684539999, -27.4681774539999, -27.4680874539999, -27.4679974439999, -27.467921454, -27.4678384439999, -27.467777444, -27.467714454, -27.467664444, -27.467623454, -27.4676094439999, -27.467604454, -27.467637434, -27.4677324539999, -27.467794484, -27.4678344439999, -27.467852444, -27.467858474, -27.467864414, -27.467875474, -27.4678814339999, -27.4678814339999, -27.467876474, -27.467882414, -27.467871444, 
-27.4678544239999, -27.467836424, -27.467825454, -27.467814464, -27.467824454, -27.467827434, -27.467825454, -27.467837424, -27.4678604539999, -27.467872424, -27.467898434, -27.4679174139999, -27.4679764639999, -27.467999404, -27.468026414, -27.468065474, -27.468076464, -27.4680944639999, -27.4681114639999, -27.4681174039999, -27.468135404, -27.468153404, -27.468171404, -27.4682014639999, -27.468204444, -27.4683944239999, -27.468483444, -27.4685164639999, -27.4685424739999, -27.4685804539999, -27.4686114039999, 
-27.468635434, -27.468644444, -27.4686573939999, -27.468674414, -27.4686904339999, -27.468689444, -27.468732454, -27.468779444, -27.468911444, -27.468814444, -27.468371444, -27.467737443, -27.467601433, -27.467288453, -27.4672354529999, -27.466969433, -27.466877433, -27.466703443, -27.466368443, -27.465991103, -27.465609443, -27.465310453, -27.4648254329999, -27.464146453, -27.4635614439999, -27.463388454, -27.463251434, -27.462826434, -27.4627554539999, -27.462194444, -27.4620304339999, -27.4619844439999, 
-27.461619444, -27.4612364339999, -27.461057454, -27.460036454, -27.459969444, -27.459644434, -27.4595704339999, -27.4594654539999, -27.4592834439999, -27.459111454, -27.458938444, -27.458753444, -27.458434444, -27.458982394, -27.459241414, -27.459736424, -27.459832214, -27.459897444, -27.459987444, -27.459837444, -27.459736364, -27.4595523639999, -27.459388364, -27.459210374, -27.458903364, -27.458540354, -27.458312344, -27.4581243439999, -27.458041454, -27.4579693539999, -27.457827354, -27.4577013639999, 
-27.4576454539999, -27.4575074439999, -27.457429324, -27.4572123149999, -27.456767305, -27.456357305, -27.455638285, -27.453664295, -27.452980295, -27.452593275)))","list()","list()","list()","list()",
                                NA),
              label = c(7L, NA, NA, NA, NA)
        )

  ggplot(survey_postcode_count, aes(geometry = geometry)) +
  geom_sf(aes(fill = unique_postcode),
          lwd = 0,
          colour = "white") +
  geom_sf_text(aes(label = postcode),size=2,check_overlap = TRUE) +
  scale_fill_gradientn(
    colors = c("#9DBF9E", "#FCB97D", "#A84268"),
    # Redefine the fill colour for NA values
    na.value = "grey80",
    # Set the out-of-bounds ("oob") rule to squish out-of-bounds values to the nearest limit
    oob = scales::squish,
    # Give the scale a title
    name = "Number of survey respondents")+
  theme_void()+
  theme(
    legend.justification = c(0, 1),
    legend.position = c(0, .95)
  )
#> Error in ggplot(survey_postcode_count, aes(geometry = geometry)): could not find function "ggplot"

Created on 2022-11-21 with reprex v2.0.2

I've tried playing around with if_else and case_when but my efforts break things.

Any help or advice would be much appreciated!

Hi @aarontimo,
I tried to run your reprex but it fails: 1) the dataframe does not read-in correctly; and 2) the provided ggplot code produces a graph frame but no contents (even when I edit the dataframe to something that looks better).

Hi @DavoWW,

Thank you for your response and I apologise for the inconvenience. After some experimentation, I think the issue is that it is an incomplete dataframe(n=5) or the nature of the geometry column. When I use the full dataframe I don't have any problems.

However, the full dataframe is so large I can't use datapasta or dput (as it over-fills the console window) to share the df.

As a potential work around, I have uploaded the df to dropbox as I can't upload it here. Here is the link to the df.

That said, I've tried downloading and importing the df using df<-read.csv("df.csv", sep='\t') and `df<-read.csv("df.csv') and I get the following error "Error in FUN(X[[i]], ...) : object 'geometry' not found" when I tried running the code. The column 'geometry' is in the csv file so I'm not sure what the error means.

I hope this helps.

Just guessing, perhaps change this line:

geom_sf_text(data = filter(survey_postcode_count, !is.na(unique_postcode)), aes(label = postcode),size=2,check_overlap = TRUE)
2 Likes

Thank you @williaml - that worked perfectly!!

Thank you very much.

1 Like

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.