旋转Treemap中的标签

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

Rotate labels in Treemap

问题

我正在使用RStudio创建一个简单的Treemap。我遇到的问题是,在地图构建时,其中一个分组非常窄,因此标签非常小而难以阅读。我想一个“简单”的解决办法就是旋转标签。但我找不到这样做的方法。是否有解决方法?

这是我的代码:

treemap(data2C,
        #data
        index="label",
        vSize="Number",
        type="index",
                
        # Main
        title="",
        
        # Borders:
        border.col=c("white"),             
        border.lwds=1,                         
        
        # Labels
        fontsize.labels=10,
        fontcolor.labels="white",
        fontface.labels=1,
        overlap.labels=0.5,
        inflate.labels=T)

这是输出的样子:
点击此处查看图像描述

提前感谢任何帮助。

英文:

I am using RStudio to build a simple Treemap. The issue I have is that when the map builds one of the groups is very narrow, therefore the label is very small and hard to read. I thought a 'simple' fix would be to just rotate the label. But I can't find a way to do this. Is there a work around?

Here is my code:

treemap(data2C,
        #data
        index="label",
        vSize="Number",
        type="index",
                
        # Main
        title="",
        
        # Borders:
        border.col=c("white"),             
        border.lwds=1,                         
        
        # Labels
        fontsize.labels=10,
        fontcolor.labels="white",
        fontface.labels=1,
        overlap.labels=0.5,
        inflate.labels=T)

Here is what the output:
enter image description here

Thanks in advance for any help

答案1

得分: 1

你可以使用ggplot2和treemapify根据绘图中的最大给定值的比例来旋转标签。在此示例中,当标签占最大值的5%以下时,将标签旋转90度。

library(treemapify)
library(ggplot2)

# 示例数据
data2C <- data.frame(
    label = c("Group A", "Group B", "Group C", "Group D", "Group E", "Group F", "Group G", "Group H", "Group I"),
    Number = c(40, 50, 30, 40, 60, 20, 30, 11, 1)
)

# 构建treemap
tm <- ggplot(data2C, aes(area = Number, fill = label, label = label)) +
    geom_treemap(color = "white", size = 1) +  # 为矩形添加白色边框
    geom_treemap_text(color = "white", size = 10, angle = ifelse(data2C$Number / max(data2C$Number) > 0.05, 0, 90)) +  # 设置标签的旋转角度和更改文本颜色为白色
    theme(legend.position = "none")  # 移除图例

# 绘制treemap
plot(tm)

输出:

旋转Treemap中的标签

你可能需要根据你的数据集调整上述代码中的值0.05来使其按你的需求工作。

如果你知道要旋转标签的确切数据点,你还可以将geom_treemap_text替换为以下内容:

geom_treemap_text(color = "white", size = 10, angle = c(0, 0, 0, 0, 0, 0, 0, 0, 90)) # 明确设置"Group I"旋转90度
英文:

You can rotate a label using ggplot2 and treemapify based on the proportion to thee maximum giveen value in the plot. This example rotates the label by 90 degrees when it's less than 5% of the maximum value.

library(treemapify)
library(ggplot2)

# Sample data
data2C &lt;- data.frame(
    label = c(&quot;Group A&quot;, &quot;Group B&quot;, &quot;Group C&quot;, &quot;Group D&quot;, &quot;Group E&quot;, &quot;Group F&quot;, &quot;Group G&quot;, &quot;Group H&quot;, &quot;Group I&quot;),
    Number = c(40, 50, 30, 40, 60, 20, 30, 11, 1)
)

# Build treemap
tm &lt;- ggplot(data2C, aes(area = Number, fill = label, label = label)) +
    geom_treemap(color = &quot;white&quot;, size = 1) +  # Add white borders to rectangles
    geom_treemap_text(color = &quot;white&quot;, size = 10, angle = ifelse(data2C$Number / max(data2C$Number) &gt; 0.05, 0, 90)) +  # Set rotation for labels and change text color to white
    theme(legend.position = &quot;none&quot;)  # Remove legend

# Plot the treemap
plot(tm)

Output:

旋转Treemap中的标签

You might have to adjust the value 0.05 in

angle = ifelse(data2C$Number / max(data2C$Number) &gt; 0.05, 0, 90)

based on your dataset to make it work as you want.

If you know the exact datapoint you want thee label to be rotated for, you can also replace the geom_treemap_text with this:

geom_treemap_text(color = &quot;white&quot;, size = 10, angle = c(0, 0, 0, 0, 0, 0, 0, 0, 90)) # Explicitly set &quot;Group I&quot; to be rotated by 90 deegrees

huangapple
  • 本文由 发表于 2023年5月29日 19:14:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356848.html
匿名

发表评论

匿名网友

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

确定