将treemap转换为ggplot [英] Converting treemap to ggplot

查看:146
本文介绍了将treemap转换为ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好消息:我可以使用treemap包创建一个漂亮的树形图。
$ b

数据:



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



树形图:



 <$ c (treemap)
tm< - treemap(forTm,
index = c(UnitGroup,Unit),
vSize =Count,
vColor =Count,
type =dens,
palette =YlGnBu,
title =#患者样本标题,
aspRatio =(1.5),
fontsize.labels = c(16,12),
fontsize.title = 16,
bg.labels =white,
border.lwds = c(5,0))

问题:class(tm)是一个列表,我需要在一个页面上用多个ggplots绘制树形图。我预计需要添加/重新排列最终用户的情节,所以我想要一个相对灵活的解决方案。

目标:将树形图插入以下仪表板:

  #just (grid = gp = gpar(fill =gray))
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(样本标题,
gp = gpar(margin = margin(10,0,10,0))),
heights = c(5,5))

但解决方案需要足够灵活,添加/重新排列图,例如如下:

  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(样本标题,
gp = gpar(margin = margin(10,0,10,0))),
heights = c(5,5))

理想情况下,我会找到一种方法将树形图重新创建为一个ggplot,因为我非常熟悉ggplot的语法,所以我可以很容易地标准化表格的风格。但如果没有办法做到这一点,我会接受任何解决方案(将treemap保存为grob?),这使我可以在我的ggplots页面中轻松地重新排列此图。



到目前为止我尝试过的是:诚实,不多。谷歌并不是我的朋友。我发现的唯一建议是使用treemapify,我不能为这个特定的项目做。

解决方案

如果您一步一步使用ggplot重构树形图,会出现什么情况?例如

  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)%>%summary(x = mean(x0 + w / 2),y = mean(y0 + h / 2),vSize = mean(vSize)),inherit.aes = F,fill = (网格)
库(gridExtra)
samplePlot< - grid.rect(gp = gpar(fill =gray),fontface =bold)

库))
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(样本标题,
gp = gpar(margin = margin(10,0,10,0))),
heights = c(5,5))


The good news: I'm able to create a beautiful treemap using the treemap package.

Data:

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))

The problem: class(tm) is a list, and I need to plot the treemap on a page with several ggplots. I anticipate needing to add/rearrange the plots for the end user, so I'd like a relatively flexible solution.

Goal: insert treemap into the following dashboard:

#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))

But the solution needs to be flexible enough that I can easily add/rearrange the plots, for example as follows:

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))

Ideally I'd find a way to recreate the treemap as a ggplot, because I'm very familiar with ggplot's syntax so I'd have an easy time standardizing the sheet's style. But if there's no way to do that I'll accept any solution(save the treemap as a grob?) that allows me to easily rearrange this graph within my page of ggplots.

What I've tried so far: Honestly, not much. Google hasn't been my friend. The only recommendation I've found is to use treemapify, which I can't do for this particular project.

解决方案

What if you reconstruct the treemap using ggplot step by step? E.g.

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))

这篇关于将treemap转换为ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆