r ggplot2 treemap

treemap r



Conversión de treemap a ggplot (1)

La buena noticia: puedo crear un hermoso treemap usando el paquete treemap.

Datos:

forTm <- structure(list( UnitGroup = c("1N", "BHU", "CSU", "ED", "Med/Surg", "Med/Surg", "Telemetry", "Telemetry", "Telemetry", "Telemetry", "Telemetry"), Unit = c("A", "B", "C", "ED", "D", "E", "F", "G", "H", "I", "J"), Count = c(1L, 1L, 1L, 1L, 15L, 10L, 5L, 2L, 3L, 8L, 4L)), class = c("data.frame"), row.names = c(NA, -11L), .Names = c("UnitGroup", "Unit", "Count"))

Treemap:

library(treemap) tm <- treemap(forTm, index = c("UnitGroup", "Unit"), vSize = "Count", vColor = "Count", type = "dens", palette = "YlGnBu", title = "# Patients Sample Title", aspRatio = (1.5), fontsize.labels = c(16, 12), fontsize.title = 16, bg.labels = "white", border.lwds = c(5,0))

El problema: class (tm) es una lista, y tengo que trazar el treemap en una página con varios ggplots. Anticipo la necesidad de agregar / reorganizar las tramas para el usuario final, por lo que me gustaría una solución relativamente flexible.

Objetivo: insertar treemap en el siguiente tablero:

#just stand-ins for the plots samplePlot <- grid.rect(gp = gpar(fill = "grey")) treemapHere <- grid.rect(gp = gpar(fill = "blue")) grid.arrange(samplePlot, # plot 1 treemapHere, # plot 2 samplePlot, # plot 3 layout_matrix = rbind(c(3, 2), c(1, 1)), top = textGrob("Sample Title", gp = gpar(margin = margin(10, 0, 10, 0))), heights = c(5, 5))

Pero la solución debe ser lo suficientemente flexible como para poder agregar / reorganizar fácilmente los gráficos, por ejemplo de la siguiente manera:

grid.arrange(samplePlot, # plot 1 samplePlot, # plot 2 samplePlot, # plot 3 samplePlot, # plot 4 treemapHere, # plot5 layout_matrix = rbind(c(1, 2, 3), c(4, 5, 5)), top = textGrob("Sample Title", gp = gpar(margin = margin(10, 0, 10, 0))), heights = c(5, 5))

Idealmente, encontraría una manera de recrear el mapa de árbol como un ggplot, porque estoy muy familiarizado con la sintaxis de ggplot, así que me sería fácil estandarizar el estilo de la hoja. Pero si no hay forma de hacerlo, aceptaré cualquier solución (¿guardar el treemap como un grob?) Que me permite reorganizar fácilmente este gráfico dentro de mi página de ggplots.

Lo que he intentado hasta ahora: Honestamente, no mucho. Google no ha sido mi amigo. La única recomendación que he encontrado es usar treemapify, que no puedo hacer para este proyecto en particular.


¿Qué pasa si reconstruyes el treemap usando ggplot paso a paso? P.ej

library(tidyverse) treemapHere <- ggplot(tm$tm, aes(xmin=x0+w, xmax=x0, ymin=y0+h, ymax=y0, fill=ifelse(level==1, NA, color), fill = vSize)) + scale_fill_identity() + geom_rect(aes( size=I((2:1)[level])), color="#303030") + coord_fixed(0.75) + guides(size="none") + cowplot::theme_nothing() + geom_text(aes(x0+w/2, y0+h/2, label=Unit, size=I(scales::rescale(vSize, c(2.5, 3))))) + geom_label(aes(x, y, label=UnitGroup, size=I(scales::rescale(vSize, c(2.5, 7)))), tm$tm %>% group_by(UnitGroup) %>% summarise(x=mean(x0+w/2), y=mean(y0+h/2), vSize=mean(vSize)), inherit.aes=F, fill="white", fontface = "bold") library(grid) library(gridExtra) samplePlot <- grid.rect(gp = gpar(fill = "grey")) grid.arrange(samplePlot, # plot 1 samplePlot, # plot 2 samplePlot, # plot 3 samplePlot, # plot 4 treemapHere, # plot5 layout_matrix = rbind(c(1, 2, 3), c(4, 5, 5)), top = textGrob("Sample Title", gp = gpar(margin = margin(10, 0, 10, 0))), heights = c(5, 5))