使用R中的Treemap包突出显示Treemap的特定部分
我使用R中的Treemap软件包来强调不同环境下的新冠病毒爆发数量。我正在使用R Markdown制作许多不同的报告。每一份报告都描述了不同类型的环境,我想在每个报告的树状图中强调这一环境,显示在所讨论的环境中发生的总暴发比例。例如,你我目前正在编写K-12学校报告,希望在图中突出显示代表该类别的方框 我以前使用的是炸圈饼图,但是有两个多的子类别,图形变得很难阅读 我正在想象一种方法来改变一个特定盒子上的标签或边框,即在盒子周围放一个黄色边框或使标签变黄。我找到了一种方法来为所有的盒子做这两件事,而不仅仅是一个特定的盒子。我使用剪贴工具进一步说明了期望的结果可能是什么样子。生成树映射的代码可以在下面的链接中找到。看起来是这样的:使用R中的Treemap包突出显示Treemap的特定部分,r,treemap,R,Treemap,我使用R中的Treemap软件包来强调不同环境下的新冠病毒爆发数量。我正在使用R Markdown制作许多不同的报告。每一份报告都描述了不同类型的环境,我想在每个报告的树状图中强调这一环境,显示在所讨论的环境中发生的总暴发比例。例如,你我目前正在编写K-12学校报告,希望在图中突出显示代表该类别的方框 我以前使用的是炸圈饼图,但是有两个多的子类别,图形变得很难阅读 我正在想象一种方法来改变一个特定盒子上的标签或边框,即在盒子周围放一个黄色边框或使标签变黄。我找到了一种方法来为所有的盒子做这两件事
# 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"
)
#库
图书馆(树形图)
#构建数据集
组看起来treemap
包没有内置的方法来实现这一点。但是我们可以通过使用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")
}
)
看起来treemap
包没有内置的方法来实现这一点。但是我们可以通过使用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")
}
)
这真是不可思议,正是我所需要的!非常感谢你!这是神奇的,正是我所需要的!非常感谢你!