将文本移近饼图的边框

Posted

技术标签:

【中文标题】将文本移近饼图的边框【英文标题】:Move text closer to borders on pie chart 【发布时间】:2021-12-08 05:48:10 【问题描述】:

我正在绘制饼图,有时扇区太小而无法容纳我的文本标签(我将值放在那里)。

    如何将所有标签移近圆的外边界(那里有更多空间)? 有没有办法让我的图例上移?图片中间有点奇怪。 也许有更多建议可以改善我的可视化效果?

这是我的代码示例:



df <- structure(list(group = c("Other", "Pool", "VNC", "File", "EtwB", 
                               "Thre", "HTab", "Vkmc", "Ntfx", "MmCa"), value = c(20.32, 6.16, 
                                                                                  4.03, 2.2, 2.05, 2.01, 1.72, 1.7, 1.56, 1.27), FileName = c("", 
                                                                                                                                              NA, "C:\\Windows\\System32\\Drivers\\netvsc.sys", NA, NA, NA, 
                                                                                                                                              NA, "C:\\Windows\\System32\\Drivers\\vmbkmcl.sys", NA, NA), Description = c("", 
                                                                                                                                                                                                                          "<unknown> (Pool tables, etc.)", "Virtual NDIS Miniport", "<unknown> (File objects)", 
                                                                                                                                                                                                                          "nt!etw (Etw Buffer)", "nt!ps (Thread objects)", "<unknown> (Hash Table pool)", 
                                                                                                                                                                                                                          "Hyper-V VMBus KMCL", NA, "nt!mm (Mm control areas for mapped files)"
                                                                                                                                              ), Legend = c("Other", "Pool - <unknown> (Pool tables, etc.)", 
                                                                                                                                                            "VNC - Virtual NDIS Miniport - C:\\Windows\\System32\\Drivers\\netvsc.sys", 
                                                                                                                                                            "File - <unknown> (File objects)", "EtwB - nt!etw (Etw Buffer)", 
                                                                                                                                                            "Thre - nt!ps (Thread objects)", "HTab - <unknown> (Hash Table pool)", 
                                                                                                                                                            "Vkmc - Hyper-V VMBus KMCL - C:\\Windows\\System32\\Drivers\\vmbkmcl.sys", 
                                                                                                                                                            "Ntfx", "MmCa - nt!mm (Mm control areas for mapped files)")), row.names = c(NA, 
                                                                                                                                                                                                                                        -10L), class = c("tbl_df", "tbl", "data.frame"))
df2 <- structure(list(group = c("Other", "Pool", "VNC", "File", "EtwB", 
                                "Thre", "HTab", "Vkmc", "Ntfx", "MmCa"), value = c(20.32, 6.16, 
                                                                                   4.03, 2.2, 2.05, 2.01, 1.72, 1.7, 1.56, 1.27), Legend = c("Other", 
                                                                                                                                             "Pool - <unknown> (Pool tables, etc.)", "VNC - Virtual NDIS Miniport - C:\\Windows\\System32\\Drivers\\netvsc.sys", 
                                                                                                                                             "File - <unknown> (File objects)", "EtwB - nt!etw (Etw Buffer)", 
                                                                                                                                             "Thre - nt!ps (Thread objects)", "HTab - <unknown> (Hash Table pool)", 
                                                                                                                                             "Vkmc - Hyper-V VMBus KMCL - C:\\Windows\\System32\\Drivers\\vmbkmcl.sys", 
                                                                                                                                             "Ntfx", "MmCa - nt!mm (Mm control areas for mapped files)"), 
                      csum = c(43.02, 22.7, 16.54, 12.51, 10.31, 8.26, 6.25, 4.53, 
                               2.83, 1.27), pos = c(32.86, 19.62, 14.525, 11.41, 9.285, 
                                                    7.255, 5.39, 3.68, 2.05, 0.635)), row.names = c(NA, -10L), class = c("tbl_df", 
                                                                                                                         "tbl", "data.frame"))



library(ggplot2)
library(tidyverse)
library(RColorBrewer)



nb.cols <- 10

mycolors <- colorRampPalette( brewer.pal(8, "Dark2"), interpolate='spline' )(nb.cols)

# Pie

font_size <- 20

plot_to_save <- 
    ggplot(df, aes(x = "", y = value, fill = fct_inorder(Legend))) +
    geom_col(width = 1, color = 1, alpha= 0.8) +
    geom_text(aes(label = value), size=4, 
              position = position_stack(vjust = 0.5), alpha = 1, color = "black") +
    coord_polar(theta = "y") +
    guides(fill = guide_legend(title = "Driver")) +
    scale_y_continuous(breaks = df2$pos, labels = df$group) +
    theme(axis.ticks = element_blank(),
          axis.title = element_blank(),
          axis.text = element_text(size = font_size-4),
          # legend.position = "none", # Removes the legend
          panel.background = element_rect(fill = "white")) +
    theme(text = element_text(family = "Segoe UI", size = font_size)) +
    #theme(legend.position = "right") +
    scale_fill_manual(values = mycolors) +
    ggtitle("Memory NonPagedUsed, MB")


ggsave(
    file = "c:\\temp\\pie_chart.png",
    plot = plot_to_save,
    width = 16,
    height = 8,
    dpi = 150,
    units = "in",
    type = "cairo-png"
)

【问题讨论】:

【参考方案1】:

您可以通过在geom_text(aes()) 中设置x 参数来实现此目的。

例如,对于您想做的事情,我会推荐:

geom_text(aes(x = 1.4, label = value), size=4, 
              position = position_stack(vjust = 0.5), alpha = 1, color = "black")

【讨论】:

以上是关于将文本移近饼图的边框的主要内容,如果未能解决你的问题,请参考以下文章

快速将字幕添加到饼图的中心

Garry 的 mod lua,将矢量移近

reactjs - 如何使用nivo Rocks为reactjs添加文本以使饼图居中?

echarts插件的饼图怎么把外围边框去掉

设置Highcharts饼图的不透明度

matplotlib饼图用主图的实际标题替换最后一个饼图子图标题[重复]