0

I have a dataset whose rows represent the time frame and whose columns each represent one region of Italy. I need to plot this data on a map of Italy in R (I need the total sum on the entire period, so I'll only have one value per region). I downloaded the package maps and loaded the map of Italy it_map <- map_data('italy') Problem is, Italy is divided into 20 regions and tens of provinces. The map shows these province boundaries, but my data is aggregated at regional level.

Is there a way to combine the boundaries of all the provinces into their respective region? Do I have to modify the "group" column in order to make it the same for all the provinces belonging to one region?

The map dataset is like this:

long lat group order region subregion
x.x  y.y   1     1   Bozen      NA
x.x  y.y   1     2   Bozen      NA

etc... where of course the column called "region" actually lists provinces.

Also, any suggestions for plotting nicely with ggplot2? Thanks in advance for all the answers!

asyd
  • 23
  • 3
  • Hi Angelo Sidonio. Please read the info about [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and how to give a [minimale reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! – dario Feb 19 '20 at 11:44

1 Answers1

0

You need a "region" column to denote the 20 regions (noting that the region column that is already in there denotes the 95 provinces as you say). Unfortunately, that particular map doesn't seem to have one.

So you'll have to either find another map that has those 20 regions as a column (did you try the mapIT package?) or try to do it yourself - but you'll need to know which provinces belong to which regions.

  1. Create a data frame based on the unique provinces:

data_regions <- data.frame(province=unique(it_map$region), Region=NA, stringsAsFactors = FALSE)

  1. Edit this data frame and fill in the values of the Region column yourself using your knowledge of Italy. Type the names (or assign a code). You should have a total of 20. This may take a bit of time.

  2. Merge this data frame with the map data you already obtained from the maps package.

    it_map <- it_map %>% rename(province=region) %>% inner_join(data_regions)

  3. Plot it.

    it_map %>% ggplot(aes(long, lat, group = group)) + geom_polygon(aes(fill = Region), color="white")

Hope that helps.

Edward
  • 10,360
  • 2
  • 11
  • 26
  • Thank you very much! I don't know why it didn't cross my mind to just add a new column instead of modyfing the existent one. Luckily, Italy's my home country so I'm able to relate provinces to regions easily, but I'd like to avoid the effort, I'll check the package you mentioned! – asyd Feb 19 '20 at 14:49