3

I am trying to work with municipality data in Norway, and I'm totally new to QGIS, shapefiles and plotting this in R. I download the municipalities from here: Administrative enheter kommuner / Administrative units municipalities

Reproducible files are here: Joanna's github

I have downloaded QGIS, so I can open the GEOJson file there and convert it to a shapefile. I am able to do this, and read the data into R:

library(sf)
test=st_read("C:/municipality_shape.shp")
head(test)

head

I have on my own given the different municipalities different values/ranks that I call faktor, and I have stored this classification in a dataframe that I call df_new. I wish to merge this "classification" on to my "test" object above, and wish to plot the map with the classification attribute onto the map:

test33=merge(test, df_new[,c("Kommunekode_str","faktor")],
             by=c("Kommunekode_str"), all.x=TRUE)

This works, but when I am to plot this with tmap,

library(tmap)
tmap_mode("view")
tm_shape(test33) + 
  tm_fill(col="faktor", alpha=0.6, n=20, palette=c("wheat3","red3")) + 
  tm_borders(col="#000000", lwd=0.2)

it throws this error:

Error in object[-omit, , drop = FALSE] : incorrect number of
dimensions

If I just use base plot,

plot(test33)

I get the picture:

result of map plot

You see I get three plots. Does this has something to do with my error above?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Joanna
  • 33
  • 4
  • Dear editor: it does not seem like the link works after the edit! Thank you! – Joanna Jul 24 '19 at 07:28
  • Hi, don't post data as images, you need to make your question [reproducible](https://stackoverflow.com/a/5963610/6574038) for Stack Overflow, cheers. – jay.sf Jul 24 '19 at 07:38
  • I'm trying to make a good question =( – Joanna Jul 24 '19 at 07:49
  • Probably you need to help us first with that we can help you. You have read my link above? Basically adding the output of `dput(test33)` to your question would help. – jay.sf Jul 24 '19 at 08:11
  • @jay.sf: Ok, I understood what you meant, I have added the files here so everything is reproducible: https://github.com/Joannagit/Municipality – Joanna Jul 24 '19 at 08:14
  • 1
    @jay.sf: Thank you for helping me! I added dput of test33 to my github repository as well! – Joanna Jul 24 '19 at 08:24
  • Thanks, brought your `dput` to work with `readRDS("dput_test33.rds")`. I think you need to add `tm_polygons()` somehow: `tmap_mode("view"); tm_shape(test33, projection="longlat") + tm_polygons() + tm_fill(col="faktor", alpha=0.6, n=20, palette=c("wheat3","red3"))`. At least Norway shows up, without colors, though. Maybe the warning helps you further :) If you don't get an answer here from anybody more familiar with this, could try asking on [GIS Stack Exchange](https://gis.stackexchange.com/help/on-topic). (Feel free to write an answer yourself if you figured it out.) – jay.sf Jul 24 '19 at 08:51
  • 1
    Thank you very much! I don't understand the error, but that is no surprise, I have no understanding of shapefiles, layers or any of this, but I appreciate the help =) – Joanna Jul 24 '19 at 09:03
  • Btw, if anybody knows any other way of plotting this, it doesn't need to be using tmap, I would be grateful=) – Joanna Jul 24 '19 at 09:04
  • @Joanna I can help with this. Can you clarify a couple of things first? 1) Would you prefer an interactive map as in your example? 2) what colour scale are you trying to achieve? Your breaks are currently using 0-1.2 in increments of 0.5, did you have a particular break scheme in mind? – Chris Jul 24 '19 at 09:13
  • Thank you so so much! I am completely new to this so I'm just trying to achieve a map where I can see the differences in "faktor" between the municipalities in a nice way. I do not know if the map should be interactive or not, and I do not know much about colour scales or break schemes. I'm so sorry that I'm answering this badly, I hope you can help me! – Joanna Jul 24 '19 at 09:19
  • Ah, it was the missing values for "faktor" that was the problem =) – Joanna Jul 24 '19 at 09:34

1 Answers1

3

I think the main issue here is that the shapes you are trying to plot are too complex so tmap is struggling to load all of this data. ggplot also fails to load the polygons.

You probably don't need so much accuracy in your polygons if you are making a choropleth map so I would suggest first simplifying your polygons. In my experience the best way to do this is using the package rmapshaper:

# keep = 0.02 will keep just 2% of the points in your polygons.
test_33_simple <- rmapshaper::ms_simplify(test33, keep = 0.02)

I can now use your code to produce the following:

tmap_mode("view")
tm_shape(test_33_simple) +
  tm_fill(col="faktor", alpha=0.6, n=20, palette=c("wheat3","red3")) + 
  tm_borders(col="#000000", lwd=0.2)

enter image description here

This produces an interactive map and the colour scheme is not ideal to tell differences between municipalities.

static version

Since you say in the comments that you are not sure if you want an interactive map or a static one, I will give an example with a static map and some example colour schemes.

The below uses the classInt package to set up breaks for your map. A popular break scheme is 'fisher' which uses the fisher-jenks algorithm. Make sure you research the various different options to pick one that suits your scenario:

library(ggplot2)
library(dplyr)
library(sf)
library(classInt)

breaks <- classIntervals(test_33_simple$faktor, n = 6, style = 'fisher')



#label breaks
lab_vec <- vector(length = length(breaks$brks)-1)
rounded_breaks <- round(breaks$brks,2)
lab_vec[1] <- paste0('[', rounded_breaks[1],' - ', rounded_breaks[2],']')
for(i in 2:(length(breaks$brks) - 1)){
  lab_vec[i] <- paste0('(',rounded_breaks[i], ' - ', rounded_breaks[i+1], ']')
}


test_33_simple <- test_33_simple %>%
  mutate(faktor_class = factor(cut(faktor, breaks$brks, include.lowest = T), labels = lab_vec))

# map
ggplot(test_33_simple) + 
  geom_sf(aes(fill = faktor_class), size= 0.2) +
  scale_fill_viridis_d() +
  theme_minimal()

enter image description here

Chris
  • 3,836
  • 1
  • 16
  • 34
  • You should look through the `style` options listed in `?classIntervals` and research a suitable class structure. Changing the class intervals can impact on the message the map portrays. – Chris Jul 24 '19 at 09:45
  • Thank you so so much! I am so happy now=) – Joanna Jul 24 '19 at 09:46