Tmap: Add bar and pie chart to choropleth map

Created on 22 Mar 2016  Â·  9Comments  Â·  Source: mtennekes/tmap

Would be great if bar and pie charts per area unit could be added to the thematic map, like here:
http://www.qgis.nl/media/2012/05/ArtikelStaafdiagram05.png

Preferrably, the bar charts would be added from a list of plots that were created by ggplot2 and the plotlist names identify to which area the bar chart belongs. The bar charts are then added to the centroids of the area unit.

enhancement

Most helpful comment

All 9 comments

Excellent idea! It would make sense to create the plots in ggplot2, and add them to the map.
As for the question where to place them: obviously, centroids can be used, but we can do better:

  • In the current tmap version, I have adapted a method to place text labels automatically. It is the function pointLabelGrid which is based on maptools::pointLabel, and uses simulated annealing. Since we can assume the insets plots to be rectangular, this function could be used for that.
  • Alternatively, @joelgombin recently had the idea to use the algorithm of the ggrepel package, which looks very promising. See https://github.com/slowkow/ggrepel/issues/24#issuecomment-198650806. I'm not sure what the pros and cons are.

I can imagine an interface like this:

tm_shape(NLD_muni) +
tm_polygons() +
tm_graphs(glist, scale=0.7)    
# where glist is a list of ggplot2 plots, one for each feature of NLD_muni

Ideally, the scale argument can also be non-constant, which allows to create proportional symbol maps.

As always, I'm short on time, but I think this should be included in the next major update. Please feel free to contribute to it.

Is this doable to overlay ggplot2 graphs on tmap? Is that because tmap uses the grid framework?

Yup

library(ggplot2)
library(tmap)

data(NLD_muni)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
qtm(NLD_muni)
print(p, vp = grid::viewport(x=.4, y=.4, width=.3, height=.3))

rplot04

man wat heb jij een fantastisch pakket in elkaar gedraaid…. Groetjes, Richard

Dr. R.L. Zijdeman | CDO | International Institute of Social History | Cruquiusweg 31 | 1019 AT Amsterdam | +31 20 668 5 866

On 23 Mar 2016, at 09:14, mtennekes <[email protected]notifications@github.com> wrote:

Yup

library(ggplot2)
library(tmap)

data(NLD_muni)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
qtm(NLD_muni)
print(p, vp = grid::viewport(x=.4, y=.4, width=.3, height=.3))

[rplot04]https://cloud.githubusercontent.com/assets/2444081/13979263/706ee1c6-f0d7-11e5-9cea-e4cfb9a1e59b.png

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHubhttps://github.com/mtennekes/tmap/issues/52#issuecomment-200239205

@mtennekes Sounds great :-) I especially like the scaling option.
Do you have an idea how to make sure that the right plot from the plot list is in it’s corresponding area unit?

Good point. I think it's the responsibility of the user. The only think that we can check automatically is that the number of plots corresponds to the number of spatial units. However, we can try to make it convenient for the user, for instance, with the following workflow:

  • A user creates small multiples with facet_wrap, where the facet-variable should also exist in the shape data (without duplicates).
  • tmap splits the facets into separate plots (anyone knows how?? I'm not familiar with ggplot2 from a devel point of view)
  • tmap matches the facet-variable with the variable in the shape data

this is certainly a way of doing it - but i think other ways should be possible as well, because facet wraps also limit the possible graphs... if you want each chart ordered in a different way, facets fail (see here: http://stackoverflow.com/questions/34001024/ggplot-order-bars-in-faceted-bar-chart-per-facet). then a loop that porduces a plot list is the way to go.

maybe, the plot list should contain an ID that matches the shape data ID - it could be the key of the list, tm_graph then looks for the corresponding list key and shape data ID

Hey there, I did not want to create a new issue since my question is related to this issue. Is it possible to create/show a bar chart or pie chart when clicking on a specific region/polygon in interactive mode?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mtennekes picture mtennekes  Â·  4Comments

Si-Chong picture Si-Chong  Â·  4Comments

jarsc568 picture jarsc568  Â·  8Comments

prosoitos picture prosoitos  Â·  5Comments

Robinlovelace picture Robinlovelace  Â·  4Comments