如何将标签放置在更远离R中的饼形图的位置?
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices,labels = lbls, col=rainbow(length(lbls)), radius=.2)
最佳答案
您可以修改pie
功能行50-54
并将其另存为新功能pie2
类型
pie
将行
50-54
更改为if (!is.na(lab) && nzchar(lab)) {
lines(c(1, 1.35) * P$x, c(1, 1.35) * P$y)
text(1.5 * P$x, 1.5 * P$y, labels[i], xpd = TRUE,
adj = ifelse(P$x < 0, 1, 0), ...)
}
更改行长(默认=
1.05
) lines(c(1, 1.35) * P$x, c(1, 1.35) * P$y)
更改因子(默认=
1.1
) text(1.5 * P$x, 1.5 * P$y, labels[i], xpd = TRUE,
adj = ifelse(P$x < 0, 1, 0), ...)
现在定义
pie2
并运行新功能pie2(slices,labels = lbls, col=rainbow(length(lbls)), radius=.2)