使用 R 中的 Treemap 包突出显示 Treemap 的特定部分
Highlight a Specific Section of a Treemap using Treemap package in R
我正在使用 R 中的 Treemap 包来突出显示不同环境中 COVID 爆发的数量。我正在使用 R Markdown 制作许多不同的报告。每一个都描述了一种不同类型的设置,我想在每份报告的树图中突出显示该设置,显示在相关设置中发生的总爆发的比例。例如你我目前正在处理 K-12 学校报告,并想在图中突出显示表示该类别的框。
我之前使用的是爆炸式甜甜圈饼图,但是有两个子类别,图表变得难以阅读。
我正在想象一种更改特定框上的标签或边框的方法,即。在盒子周围放置黄色边框或将标签设为黄色。我找到了一种方法来为所有盒子做这两种事情,而不仅仅是一个特定的盒子。我使用截图工具制作了 this image 以进一步说明所需的结果。生成树状图的代码可以在下面的 link 中找到。它看起来像这样:
# library
library(treemap)
# Build Dataset
group <- c(rep("group-1",4),rep("group-2",2),rep("group-3",3))
subgroup <- paste("subgroup" , c(1,2,3,4,1,2,1,2,3), sep="-")
value <- c(13,5,22,12,11,7,3,1,23)
data <- data.frame(group,subgroup,value)
# treemap
treemap(data,
index=c("group","subgroup"),
vSize="value",
type="index"
)
这是我能找到的关于包的最直接的信息,这是我从中获取示例图像和代码的地方:https://www.r-graph-gallery.com/236-custom-your-treemap.html
treemap
包似乎没有 built-in 方法来执行此操作。但是我们可以通过使用 treemap()
返回的数据框并在适当的视口中添加一个矩形来破解它。
# Plot the treemap and save the data used for plotting.
t = treemap(data,
index = c("group", "subgroup"),
vSize = "value",
type = "index"
)
# Add a rectangle around subgroup-2.
library(grid)
library(dplyr)
with(
# t$tm is a data frame with one row per rectangle. Filter to the group we
# want to highlight.
t$tm %>%
filter(group == "group-1",
subgroup == "subgroup-2"),
{
# Use grid.rect to add a rectangle on top of the treemap.
grid.rect(x = x0 + (w / 2),
y = y0 + (h / 2),
width = w,
height = h,
gp = gpar(col = "yellow", fill = NA, lwd = 4),
vp = "data")
}
)
我正在使用 R 中的 Treemap 包来突出显示不同环境中 COVID 爆发的数量。我正在使用 R Markdown 制作许多不同的报告。每一个都描述了一种不同类型的设置,我想在每份报告的树图中突出显示该设置,显示在相关设置中发生的总爆发的比例。例如你我目前正在处理 K-12 学校报告,并想在图中突出显示表示该类别的框。
我之前使用的是爆炸式甜甜圈饼图,但是有两个子类别,图表变得难以阅读。
我正在想象一种更改特定框上的标签或边框的方法,即。在盒子周围放置黄色边框或将标签设为黄色。我找到了一种方法来为所有盒子做这两种事情,而不仅仅是一个特定的盒子。我使用截图工具制作了 this image 以进一步说明所需的结果。生成树状图的代码可以在下面的 link 中找到。它看起来像这样:
# library
library(treemap)
# Build Dataset
group <- c(rep("group-1",4),rep("group-2",2),rep("group-3",3))
subgroup <- paste("subgroup" , c(1,2,3,4,1,2,1,2,3), sep="-")
value <- c(13,5,22,12,11,7,3,1,23)
data <- data.frame(group,subgroup,value)
# treemap
treemap(data,
index=c("group","subgroup"),
vSize="value",
type="index"
)
这是我能找到的关于包的最直接的信息,这是我从中获取示例图像和代码的地方:https://www.r-graph-gallery.com/236-custom-your-treemap.html
treemap
包似乎没有 built-in 方法来执行此操作。但是我们可以通过使用 treemap()
返回的数据框并在适当的视口中添加一个矩形来破解它。
# Plot the treemap and save the data used for plotting.
t = treemap(data,
index = c("group", "subgroup"),
vSize = "value",
type = "index"
)
# Add a rectangle around subgroup-2.
library(grid)
library(dplyr)
with(
# t$tm is a data frame with one row per rectangle. Filter to the group we
# want to highlight.
t$tm %>%
filter(group == "group-1",
subgroup == "subgroup-2"),
{
# Use grid.rect to add a rectangle on top of the treemap.
grid.rect(x = x0 + (w / 2),
y = y0 + (h / 2),
width = w,
height = h,
gp = gpar(col = "yellow", fill = NA, lwd = 4),
vp = "data")
}
)