install.packages("plotrix")
Below is an example of a basic plot
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
#calculate the average score of each grade
pie3D(grade)
How can we add a label to the pie chart?
1. We need to know the names of the labels.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
names(grade)
#The names of the labels should be "A" "B" "C" "F"
2. We need a "package" to hold these labels
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade))
lbls
#paste: Concatenate vectors after converting it to a character. In this case, it holds the values A B C F
3.Finally we combine the chart and the labels together.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade))
pie3D(grade,labels=lbls)
How can we add the percentage of each piece together with the labels?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(grade))
#format function holds the percentage
pie3D(grade,labels=lbls)
The labels seem too messy, right? How can we limit the decimal place to reduce the labels space?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(round(grade,2)))
#round(number, the number of decimal places)
pie3D(grade,labels=lbls)
How can we change the size of the labels?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(round(grade,2)))
pie3D(grade,labels=lbls,labelcex=1)
#labelcex changes the size of the label
How can we change the color of the labels?
For example we want to change the color to red.
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(round(grade,2)))
pie3D(grade,labels=lbls,labelcol=2)
#labelcol represents the color of the label
The pie chart looks kind of small, how can we enlarge it?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(round(grade,2)))
pie3D(grade,radius=1.5,labels=lbls)
#the original radius is 1.
How can we change the height of the pie chart?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(round(grade,2)))
pie3D(grade,height=0.3,labels=lbls)
#the original height is 0.1
How can we rotate the whole chart vertically?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(round(grade,2)))
pie3D(grade,theta=pi/3,labels=lbls)
#rotate it with pi/3 angle
How can we rotate the whole chart based on the axes?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(round(grade,2)))
pie3D(grade,start=1,labels=lbls)
#The value of "start" determines how to rotate the chart based on the axes.
How can we make each piece of the pie chart more scattered?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(round(grade,2)))
pie3D(grade,labels=lbls,explode=0.1)
#The value of explode determines how scattered all pieces are.
How can we add title to the pie chart?
moody <- read.csv("https://raw.githubusercontent.com/kunal0895/RDatasets/master/Moody2018.csv")
library(plotrix)
grade <- tapply(moody$SCORE,moody$GRADE,mean)
lbls <- paste(names(grade),format(round(grade,2)))
pie3D(grade,labels=lbls,main="score vs grade")