How to place the labels further away from pie chart in 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)
You can modify the pie
-function line 50-54
and save it as a new function pie2
Type
pie
Change line 50-54
to
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), ...)
}
Change line length (default = 1.05
)
lines(c(1, 1.35) * P$x, c(1, 1.35) * P$y)
Change the factor (default = 1.1
)
text(1.5 * P$x, 1.5 * P$y, labels[i], xpd = TRUE,
adj = ifelse(P$x < 0, 1, 0), ...)
Now define pie2
and run the new function
pie2(slices,labels = lbls, col=rainbow(length(lbls)), radius=.2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With