如何(自动)使用ggplot2和treemapify向treemap添加自定义图标?

huangapple go评论61阅读模式
英文:

How to (automatically) add custom icon to a treemap using ggplot2 and treemapify?

问题

以下是代码的翻译部分:

我正在尝试通过矩形树图的方式来展示土地覆盖数据。我成功创建了一个简单的矩形树图,并“获取”了一些图标。不幸的是,我难以将这些图标包含到矩形树图中,这将有助于展示土地覆盖的类型。

这是一个示例代码:

    # 创建数据框
    df <- data.frame(category = c("森林", "绿地", "农田"),
                     面积 = c(20, 10, 70)
    )
    df$标签 = paste0(df$area, "%")
    
    # 加载图标
    library(magick)
    icon_crops2 <- image_read("https://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Agriculture_-_The_Noun_Project.svg/512px-Agriculture_-_The_Noun_Project.svg.png")
    icon_forest2 <- image_read("https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/BSicon_FOREST.svg/500px-BSicon_FOREST.svg.png")
    icon_greenland2 <- image_read("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Wikidata_Morse_code_logo_grassroots.svg/600px-Wikidata_Morse_code_logo_grassroots.svg.png")
    
    # 将图标添加到数据框
    df$icons = c(icon_crops2, icon_forest2, icon_greenland2)
    print(df$icons)
    
    # 绘制不带图标的矩形树图
    library(ggplot2)
    library(treemapify)
    
    ggplot(df, aes(area = area, 
                         fill = category, 
                         label = paste(category,label,sep="\n"),
                         subgroup = category)) +
      geom_treemap() +
      geom_treemap_text(colour = "black", place = "center") +
      theme(legend.position = "none")

希望这对你有所帮助。

英文:

I'm trying to illustrate land cover data by the means of a treemap. I managed to create a simple treemap and to "grab" some icons. Unfortunately, I struggle to include these icons into the treemap which would help to illustrate the type of land cover.

This is an example code:

# create dataframe
df &lt;- data.frame(category = c(&quot;forest&quot;, &quot;greenland&quot;, &quot;crops&quot;),
                 area = c(20, 10, 70)
)
df$label = paste0(df$area,&quot;%&quot;)

# load icons
library(magick)
icon_crops2 &lt;- image_read(&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Agriculture_-_The_Noun_Project.svg/512px-Agriculture_-_The_Noun_Project.svg.png&quot;)
icon_forest2 &lt;- image_read(&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/BSicon_FOREST.svg/500px-BSicon_FOREST.svg.png&quot;)
icon_greenland2 &lt;- image_read(&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Wikidata_Morse_code_logo_grassroots.svg/600px-Wikidata_Morse_code_logo_grassroots.svg.png&quot;)

# add icons to dataframe
df$icons = c(icon_crops2, icon_forest2, icon_greenland2)
print(df$icons)

# plot treemap without icons
library(ggplot2)
library(treemapify)

ggplot(df, aes(area = area, 
                     fill = category, 
                     label = paste(category,label,sep=&quot;\n&quot;),
                     subgroup = category)) +
  geom_treemap() +
  geom_treemap_text(colour = &quot;black&quot;, place = &quot;center&quot;) +
  theme(legend.position = &quot;none&quot;)

This results in the following image:
如何(自动)使用ggplot2和treemapify向treemap添加自定义图标?

Yet, I would like to have something like this (created with GIMP):
如何(自动)使用ggplot2和treemapify向treemap添加自定义图标?

Although I could insert the icons manually, e.g. by using GIMP, I'm looking for an automatic way.

Anyone has an idea how to do this?

答案1

得分: 2

以下是翻译好的代码部分:

# 创建数据框
df <- data.frame(
  category = c("forest", "greenland", "crops"),
  area = c(20, 10, 70)
)
df$label <- paste0(df$area, "%")

# 绘制不带图标的树状图
library(ggplot2)
library(treemapify)
library(ggimage)

dat_image <- treemapify(df,
  area = "area",
  layout = "squarified",
  start = "bottomleft"
) %>%
  transform(x = .5 * (xmin + xmax), y = .5 * (ymin + ymax))

dat_image$icons <- c(
  "https://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Agriculture_-_The_Noun_Project.svg/512px-Agriculture_-_The_Noun_Project.svg.png",
  "https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/BSicon_FOREST.svg/500px-BSicon_FOREST.svg.png",
  "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Wikidata_Morse_code_logo_grassroots.svg/600px-Wikidata_Morse_code_logo_grassroots.svg.png"
)

ggplot(df, aes(
  area = area,
  fill = category,
  label = label,
  subgroup = category
)) +
  geom_treemap() +
  geom_treemap_text(colour = "black", place = "center") +
  scale_x_continuous(limits = c(0, 1), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
  geom_image(data = dat_image, aes(x = x, y = y, image = icons), inherit.aes = FALSE, nudge_y = .1, size = .1) +
  theme(
    legend.position = "none",
    axis.line = element_blank(),
    axis.text = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank()
  )

上面的代码是使用ggimage::geom_image以及treemapify::treemapify()创建包含矩形坐标的数据集,并将图像放置在正确的矩形中的示例代码。如果您需要更多帮助或有其他问题,请随时提出。

英文:

One option would be to use ggimage::geom_image. To place the images we could use treemapify::treemapify() to create a dataset with the coordinates of the rectangles which could then be passed to geom_image to place the images in the right rectangles:

# create dataframe
df &lt;- data.frame(
  category = c(&quot;forest&quot;, &quot;greenland&quot;, &quot;crops&quot;),
  area = c(20, 10, 70)
)
df$label &lt;- paste0(df$area, &quot;%&quot;)

# plot treemap without icons
library(ggplot2)
library(treemapify)
library(ggimage)

dat_image &lt;- treemapify(df,
  area = &quot;area&quot;,
  layout = &quot;squarified&quot;,
  start = &quot;bottomleft&quot;
) |&gt;
  transform(x = .5 * (xmin + xmax), y = .5 * (ymin + ymax))

dat_image$icons &lt;- c(
  &quot;https://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Agriculture_-_The_Noun_Project.svg/512px-Agriculture_-_The_Noun_Project.svg.png&quot;,
  &quot;https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/BSicon_FOREST.svg/500px-BSicon_FOREST.svg.png&quot;,
  &quot;https://upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Wikidata_Morse_code_logo_grassroots.svg/600px-Wikidata_Morse_code_logo_grassroots.svg.png&quot;
)

ggplot(df, aes(
  area = area,
  fill = category,
  label = label,
  subgroup = category
)) +
  geom_treemap() +
  geom_treemap_text(colour = &quot;black&quot;, place = &quot;center&quot;) +
  scale_x_continuous(limits = c(0, 1), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
  geom_image(data = dat_image, aes(x = x, y = y, image = icons), inherit.aes = FALSE, nudge_y = .1, size = .1) +
  theme(
    legend.position = &quot;none&quot;,
    axis.line = element_blank(),
    axis.text = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank()
  )

如何(自动)使用ggplot2和treemapify向treemap添加自定义图标?<!-- -->

huangapple
  • 本文由 发表于 2023年3月7日 00:40:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定