r - 将文本移近饼图的边框
问题描述
我正在绘制饼图,有时扇区太小而无法容纳我的文本标签(我将值放在那里)。
- 如何将所有标签移近圆圈的外边界(那里有更多空间)?
- 有什么办法可以让我的传奇上移吗?图片中间有点奇怪。
- 也许有更多建议来改善我的可视化?
这是我的代码示例:
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"
)
解决方案
x
您可以通过在 中设置参数来实现此目的geom_text(aes())
。
对于您想要做的事情,我建议例如:
geom_text(aes(x = 1.4, label = value), size=4,
position = position_stack(vjust = 0.5), alpha = 1, color = "black")
推荐阅读
- c - C - 从双向链表中删除任何节点
- asp.net-core - 如何使需要授权登录/注销的 SignalR 集线器?
- mysql - 用另一个字符串替换字符串的一部分,而不总是相同的初始字符串?
- postgresql - 在 PostgreSQL 中返回数据类型函数
- presto - 错误:Presto 服务器仍在初始化
- javascript - 更改事件的颜色 Fullcalendar
- android - 谁能告诉我如何导入从关联 android 开发人员考试下载的项目
- python - 包含数组的 ctypes 结构
- c# - Draw and rotate the text along z axis in opengl/opentk
- c# - 使用 AutoFixture 进行单元测试以保证返回的对象列表中有一个条目