r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
852 stars 120 forks source link

Add bar and pie chart to choropleth map #52

Closed marionowak closed 7 years ago

marionowak commented 8 years ago

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](like this)

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.

mtennekes commented 8 years ago

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:

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.

joelgombin commented 8 years ago

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

mtennekes commented 8 years ago

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

rlzijdeman commented 8 years ago

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 notifications@github.com<mailto: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

marionowak commented 8 years ago

@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?

mtennekes commented 8 years ago

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:

marionowak commented 8 years ago

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

mtennekes commented 7 years ago

It's working!

rplot13

See https://github.com/mtennekes/tmap/blob/master/examples/tm_symbols.R

manuelwaltschek commented 6 years ago

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?

jeffcsauer commented 3 years ago

EDIT: Temporary solution was to increase geom_bar(width=2, ...) to 2. This fills the whitespace.

Super useful feature! Had a quick question as I am using this in a 2021 workflow. I've noted that I am getting some strange black borders on each of the subplots (see picture from linked repress):

image

I have tried adding the following lines to the chunk that builds the gross these lines in the `grob':

...
theme(panel.grid = element_blank(),
panel.background = element_rect(color = NA), 
plot.background = element_rect(color = NA),
theme(plot.margin=grid::unit(c(0,0,0,0), "mm")))