Home > Back-end >  Different rendering between chloroplethr package and ggplot2
Different rendering between chloroplethr package and ggplot2

Time:10-24

I got a visualisation problem and I can't get my head around. From some data with position information included I want to create a map. I found the great choroplethr package which was a great starting point and it really helped me a lot to understand how to process data for meaningful results. Here is the map the way I would like to have it: Figure with choroplethr

But when I try to replicate the steps (cf. self$render of the chlorplethr package using ggplot2) I get the following result: Figure with ggplot2

Does anyone have an idea where my parameters are wrong/lacking something? Here is the code:

fig <- ggplot(data=merge.shp, aes(x = long, y = lat, group = group))  
    geom_polygon(aes(fill = sqm_cat), na.rm=FALSE, rule="evenodd", position="identity")  
    coord_equal()  
    scale_fill_brewer("", drop = FALSE, na.value = "black")   
    ggplot2::theme_void()  
    ggtitle("People per square kilometers")

Edit: I think, I found the problem. If I just plot the map for one region

merge.shp %>% filter(plz %in% c("645")) 
        %>% ggplot(aes(x=long, y=lat, group=group))   geom_path

I get the following result:

geom_path results

So everything may be related to the "wrong" connection of coordinates. If I replace geom_path with geom_point there are some reasonable outlines. But how do I translate this to the map?

CodePudding user response:

The solution is actually quite simple. The order is essential for geom_polygon and I was mistakenly assuming my dataframe merge.shp was in an ascending order for all factors, which it wasn't. Introducing

merge.shp = merge.shp[order(merge.shp$order), ]

made it work.

  • Related