R Pie Charts
R language provides a large number of libraries to implement plotting functions.
A pie chart, also known as a pie graph, is a circular statistical chart divided into several sectors, used to describe the relative relationship between quantities, frequencies, or percentages.
R language uses the pie() function to create pie charts. The syntax is as follows:
pie(x, labels = names(x), edges = 200, radius = 0.8, clockwise = FALSE, init.angle = if(clockwise) 90 else 0, density = NULL, angle = 45, col = NULL, border = NULL, lty = NULL, main = NULL, β¦)
* x: A numeric vector representing the area of each sector.
* labels: A character vector representing the labels for each sector area.
* edges: This parameter has little use, it refers to the number of edges of the polygon (the circle's outline is similar to a polygon with many edges).
* radius: The radius of the pie chart.
* main: The title of the pie chart.
* clockwise: A logical value indicating whether the pie chart slices are divided in clockwise direction.
* angle: Sets the angle of the shading.
* density: The density of the shading. The default value is NULL.
* col: Represents the color of each sector, equivalent to a palette.
To draw a pie chart, you need to prepare: a vector reflecting the quantity, labels for each part, and colors for each part (optional).
Next, we will draw a simple pie chart:
## Example
# Data preparation
info = c(1, 2, 4, 8)
# Naming
names = c("Google", "Tutorial", "Taobao", "Weibo")
# Coloring (optional)
cols = c("#ED1C24","#22B14C","#FFC90E","#3f48CC")
# Plotting
pie(info, labels=names, col=cols)
When executing the plotting program, a PDF file (Rplots.pdf) will be generated in the current directory. Opening the file, you can see the graphics effect as follows:
!(#)
We can also use png(), jpeg(), bmp() functions to set the output file format to images:
## Example
# Data preparation
info =c(1, 2, 4, 8)
# Naming
names=c("Google", "Tutorial", "Taobao", "Weibo")
# Coloring (optional)
cols =c("#ED1C24","#22B14C","#FFC90E","#3f48CC")
# Set output image
png(file='tutorial-pie.png', height=300, width=300)
# Plotting
pie(info, labels=names, col=cols)
Next, we will set a title for the pie chart. For Chinese fonts, you need to set the font parameter family='GB1', or you can set your own font library. For details, refer to: (#).
## Example
# Data preparation
info = c(1, 2, 4, 8)
# Naming
names = c("Google", "Tutorial", "Taobao", "Weibo")
# Coloring (optional)
cols = c("#ED1C24","#22B14C","#FFC90E","#3f48CC")
# Calculate percentage
piepercent = paste(round(100*info/sum(info)), "%")
# Plotting
pie(info, labels=piepercent, main = "Website Analysis", col=cols, family='GB1')
# Add color sample legend
legend("topright", names, cex=0.8, fill=cols)
!(#)
To draw a 3D pie chart, you can use the pie3D() function from the plotrix library. Before using it, we need to install it first:
install.packages("plotrix", repos = "https://mirrors.ustc.edu.cn/CRAN/")
## Example
# Load plotrix
library(plotrix)
# Data preparation
info =c(1, 2, 4, 8)
# Naming
names=c("Google", "Tutorial", "Taobao", "Weibo")
# Coloring (optional)
cols =c("#ED1C24","#22B14C","#FFC90E","#3f48CC")
# Set file name, output as png
png(file="3d_pie_chart.png")
# Draw 3D chart, family should be set to the Chinese font library supported by your system
pie3D(info,labels=names,explode =0.1, main ="3D Map",family="STHeitiTC-Light")
The generated image is as follows:
!(#)
YouTip