1

I'm drawing a geographical map of my country using ggplot2. Actually, I've got what I was looking for, but I want something more in the legend. I have a dataset like this:

City Name   Nominal Wage   Real Wage
Rome        1200           15
Milan       1500           16
Naples      700            12
....        ....           ...    

I want to draw the map of my country and fill the regions around some cities according with specific values for Real Wage that I set. The script that I'm using is this ( "prov2022" is a shapefile for the geographic map)

right_join(prov2022, dataset, by = "COD_PROV") %>% 
  ggplot(aes(fill = `Real Wage`))+
  geom_sf(data = ~ dplyr::mutate(., `Real Wage` = ifelse(`Real Wage` >= 15 & `Real Wage` <= 16, `Real Wage`, `Real Wage`[NA]))) + 
  theme_void() +
  scale_fill_gradientn(colors =  'blue', na.value = "#00000000") 

It works perfectly, but the legend that I get contains as title the name of the variable Real Wage and the scale of colors used to fill the areas. Here the screenshot

Legend

Is there a way to remove that useless scale of colors ( because i'm using just one to fill) and create a legend instead that contains the City Name and the values of the variable Nominal Wage next to the specific values of Real Wage of the areas filled???

r2evans
  • 141,215
  • 6
  • 77
  • 149
io_boh
  • 193
  • 7
  • 1
    1) make your data long, i.e. bring nominal wage and real wage into one column and the values in another see https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph – tjebo Feb 11 '23 at 17:31
  • 1
    2) in `aes`, don't use backticks, but the unquoted name of the new column that contains the values "nominal wage" and "real wage" (or the values column if you want to show a continuous fill) – tjebo Feb 11 '23 at 17:32
  • hi @tjebo and thank for the answer. If I try to make the data long using the pivot_longer, I get this error <> – io_boh Feb 11 '23 at 17:40
  • I'm using shapefiles on ggplot and I also get this answer from R < and `geometry` . Run `rlang::last_error()` to see where the error occurred.>>. – io_boh Feb 11 '23 at 17:43
  • you need to select the correct columns, e.g. your code could be `pivot_longer(df, cols = c(Nominal Wage, Real Wage), "var", "value")` btw, with spaces in column names you're making your life more difficult. Here in the code you would need backticks, but I cannot print them with markdown in the comment. (It's possible, but I keep forgetting how) – tjebo Feb 11 '23 at 17:45
  • @tjebo ok, thanks. Is there a way instead to use a ggplot2 specification (such as `theme` ) and write manually the content of the legend?? – io_boh Feb 11 '23 at 17:55
  • 2
    the way above is the most direct and easiest way. If you don't manage to get this done, you will need to provide a bit more of your data, so to reproduce this – tjebo Feb 11 '23 at 18:01
  • `that contains the City Name and the values of the variable Nominal Wage next to the specific values of Real Wage` sounds more like a table than a legend, is that what you're going for? (If so, `gridExtra::tableGrob` perhaps?) – r2evans Feb 11 '23 at 18:12
  • hi @r2evans , maybe yes.. it is more a table than a legend, also because i'm using only one variable to fill the areas in the plot but at the same time I want also the others (that I'm not using) in the legend. This is what I'm tring to make https://i.stack.imgur.com/la46p.png – io_boh Feb 11 '23 at 18:24
  • @r2evans is that possible to have ( or add) this kind of table in the legend of ggplot? – io_boh Feb 11 '23 at 18:32
  • Unfortunately this has been asked (on SO) many times, and I've tried to do it elegantly as a native ggplot2-legend, and ... to no avail. I think the way I've seen users resort to is making a `tableGrob` and either `ggarrange` or `patchwork` to combine it with another grob. I hope I'm missing something, since I'd _really_ like to be able to use that myself. Any other workaround involves using a legend, crafting the text such that the various columns _sort of_ line up vertically, but it is not good/clean nor guaranteed. – r2evans Feb 11 '23 at 18:58
  • 1
    maybe https://stackoverflow.com/questions/70511777/is-it-possible-to-combine-a-ggplot-legend-and-table might help – tjebo Feb 11 '23 at 19:46
  • 1
    @tjebo two +1 for that link: the initiative to use `family="mono"`, and for `label.position="left"` – r2evans Feb 12 '23 at 19:49
  • hi @r2evans. I've followed your suggestions and I'm using `tableGrob` to make a table and `grid.arrange` to plot it next to my geographical map. This is the command I'm using `grid.arrange(arrangeGrob(geo_map, table, ncol=2))`. In this way I have the map on the right and the table on the left, which is what i was looking for. The problem is that there is too much empty space between them in the middle, while the table in the right is cut. Is there a something that I can set in `grid.arrange` to reduce the empty space or move directly the table a little more on the left? – io_boh Feb 26 '23 at 21:21

0 Answers0