Create pie charts in R

r
datavisualization

#1

Hello,

Can we create Pie Charts in R? If yes, what function/package to use for the same.

Thanks


#2

hello @Steve,

I will use the iris dataset for illustration:

vec <- iris$Species
vec_data <- data.frame(table(vec))
# create colors for the chart:
cols<- c("grey90","white","black")
# Calculate percentages of each species for displaying labels:
percentlabel = round(100*vec_data$Freq/sum(vec_data$Freq),1)
# create pie labels with % sign:
pielabel = paste(percentlabel,"%",sep = "")

# Create pie-chart:
pie(vec_data$Freq,labels = pielabel,col = cols)

# Give legends:
legend("topright",c("setosa","versicolor","virginica"),fill = cols,cex = 0.8) 

Hope this helps!!


#3

just use

 pie(table(iris$Species))

note table(iris$Species) would get the output in terms of various factors/levels

table(iris$Species)

setosa versicolor  virginica 
    50         50         50