使用R中的pie
功能,标签显示在饼图之外。有没有办法在饼图的每个切片中放置标签?
f=table(LETTERS[c(rep(1,7),rep(2,10),rep(3,5))])
pie(f)
它显示了如何使用ggplot
place-labels-on-pie-chart来完成此操作。使用pie
函数无法做到吗?
编辑:在回应关于使用饼图的一些评论时,我想详细说明我的意图。我实际上只是使用饼图作为散点图矩阵的一部分来可视化整个数据框以进行回归。矩阵设置如下:
看,我在对角线上需要一些因子,并在饼图上决定。我决定不管怎样留下标签,也取消了所有轴标签。我更倾向于使用较暗的颜色来制作更大的饼图...所以也许它可能是显示信息的一种不好的方式,但我认为它对于因子变量很有效,显示观察结果在因子水平上的比例优于条形图。下图描述了lars
包中的糖尿病数据集。
答案 0 :(得分:4)
我不认为有一种简单的方法可以做到这一点,因为标签位置是硬编码的 - 看看结尾
body(pie)
if (!is.na(lab) && nzchar(lab)) {
lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y)
text(1.1 * P$x, 1.1 * P$y, labels[i], xpd = TRUE,
adj = ifelse(P$x < 0, 1, 0), ...)
}
}
title(main = main, ...)
invisible(NULL)
}
但是你可以覆盖程序的这一部分
# create a new pie function to save overwriting original
newpie <- pie
# Tweak the label positions - changed 1.1 to 0.7
# also commented out the lines function - so that the
# small lines next to the labels are not plot
newlbs <- quote(if (!is.na(lab) && nzchar(lab)) {
#lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y)
text(0.7 * P$x, 0.7 * P$y, labels[i], xpd = TRUE,
adj = ifelse(P$x < 0, 1, 0), ...)
})
# add in the new lines of code - trial and error found the right position
body(newpie)[[22]][[4]][[7]] <- newlbs
newpie(f)
所以看起来工作量很低但是它到了那里你需要做更多的工作来调整或格式化文本。
(可能会有一个论据要做到这一点)