Chapter 11 Default Pie Charts in R
Formula:
pie(x, labels = names(x), radius = 0.8)
- x: a vector of non-negative numerical quantities. The values in x are displayed as the areas of pie slices.
- labels: character strings giving names for the slices.
- radius: radius of the pie circle. If the character strings labeling the slices are long it may be necessary to use a smaller radius.
Basic pie charts: -> pie()
Example-1:
df <- data.frame(
group = c("Male", "Female", "Child"),
value = c(35, 45, 20)
)
df
## group value
## 1 Male 35
## 2 Female 45
## 3 Child 20
pie(df$value, labels = df$group, radius = 1,col=c("#AA6F39","#2F4073","#2B823A"))
Example-2:
# Change colors
pie(df$value, labels = df$group, radius = 1,
col = c("#5CCDC9", "#DB62C4", "#FFED73"))
Example-3:
# Pie Chart with Percentages
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)),
main="Pie Chart of Countries")
Example-4:
# Pie Chart from data frame with Appended Sample Sizes
mytable <- table(iris$Species)
lbls <- paste(names(mytable), "\n", mytable, sep="")
pie(mytable, labels = lbls,
main="Pie Chart of Species\n (with sample sizes)")