1

I'm trying to draw 2 maps using a different dataset for each one of them, but the same shapefiles.

This is an example of dataset 1

#dataset1
COD_PROV   Real Wage
1         530
1         520
1         410
2         300
2         205
2         501
13        700
13        800   
13        900
18        440
18        590   
18        620
19        340
19        590   
19        320

and this is and example of dataset2

COD_PROV  PPP Wage 
1         130
1         620
1         510
2         400
2         255
2         601
13        754
13        600   
13        950
18        350
18        690   
18        520
19        640
19        790   
19        720

The shapefiles are in a directory and I've no ideaa how to upload them. However, you are going to find them in the script as prov2022.

COD_PROV is the code of each area of the map

This is the script I'm using for MAP 1

right_join(prov2022, dataset1, by = "COD_PROV") %>% 
  ggplot(aes( fill = `Real Wage` >= 400 & `Real Wage` <= 800)) +
  geom_sf() +
  theme_void() +
  theme(legend.position = "none", legend.title=element_blank())+
  scale_fill_manual(values = c('white', 'orange')) 

and this is the script I'm using for MAP 2 ( the shapefiles are the same, but the dataset is different)

right_join(prov2022, dataset2, by = "COD_PROV") %>% 
  ggplot(aes( fill =  `PPP Wage` >= 400 & `Real Wage` <= 800)) +
  geom_sf() +
  theme_void() +
  theme(legend.position = "none", legend.title=element_blank())+
  scale_fill_manual(values = c('white', 'orange')) 

Is there a way to overlap these 2 maps in a unique one, but leaving filled only the areas that have the values in common (in this case when Real Wage >= 400 & Real Wage < 800 coincides with PPP Wage >= 400 & PPP Wage < 800 ) and left all the other uncolored (or white) ?

To give you an example

MAPS

The orange ones are the MAPS of my script, while the red one is what I'm looking for... Is it possible to do it?

io_boh
  • 193
  • 7
  • 1
    Basically you could join your datasets so that you end up with one dataset containing both columns. Afterwards you could use the approach I offered on a [former question](https://stackoverflow.com/questions/75523447/ggplot-is-it-possible-to-overlap-2-plots) from you. However, one issue is that you datasets contain multiple obs. or values per `COD_PROV` which I would suggest to fix first, e.g. even with just one dataset only one ob the observations will show up in the map. – stefan Feb 26 '23 at 18:15
  • 1
    hi @stefan! yes, i've realized after that I could have done it in that way. I had problem in merging datasets because some COD_PROV values were missing, but in another 'question' a user helped me suggesting to use `[match(datset1$COD_PROV, dataset2$COD_PROV)]`. I've set then`fill = ..` with the conditions i was interested and it has worked – io_boh Feb 26 '23 at 20:30

0 Answers0