Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place the labels further from pie chart

Tags:

r

pie-chart

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)

pie chart example

like image 549
wittich Avatar asked Dec 19 '22 23:12

wittich


1 Answers

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)

enter image description here

like image 129
harre Avatar answered Jan 09 '23 11:01

harre