用R绘制饼图

  • Simple Pie Chart

Script

1
2
3
slices <- c(10, 12,4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie(slices, labels = lbls, main="Pie Chart of Countries")

Output
pie1

  • Pie Chart with Annotated Percentages

Script

1
2
3
4
5
6
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")

Output
pie2

  • 3D Pie Chart

Script

1
2
3
4
library(plotrix)
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie3D(slices,labels=lbls,explode=0.1, main="Pie Chart of Countries ")

Output
pie3

  • Creating Annotated Pies from a Dataframe

Script

1
2
3
mytable <- table(iris$Species)
lbls <- paste(names(mytable), "n", mytable, sep="")
pie(mytable, labels = lbls, main="Pie Chart of Speciesn (with sample sizes)")

Output
pie4