R 语言饼状图
饼状图
饼状图是数据的圆形视图。
使用 pie() 函数绘制饼状图:
实例
bitmap(file="out.png")# Create a vector of piesx <- c(10,20,30,40)# Display the pie chartpie(x)
结果如下:

实例解释
正如您所见,饼图为向量中的每个值绘制一个饼状图(在本例中为 10、20、30、40)。
默认情况下,第一个饼图的绘制时从 x-轴 开始并 逆时针移动。
注意:每个饼图的大小是通过使用以下公式将该值与所有其他值进行比较来确定的:
值除以总值(所有值之和):
x/sum(x)起始角度
您可以使用 init.angle 参数来更改饼图的起始角度。
init.angle 的值由角度定义,以度为单位,其中默认角度为 0。
实例
在90度开始第一个饼图:
bitmap(file="out.png")# Create a vector of piesx <- c(10,20,30,40)# Display the pie chart and start the first pie at 90 degreespie(x, init.angle = 90)
结果如下:

标题与标签
使用 label 参数将标签添加到饼图中,并使用 main 参数添加标题:
实例
bitmap(file="out.png")# Create a vector of piesx <- c(10,20,30,40)# Create a vector of labelsmylabel <- c("Apples", "Bananas", "Cherries", "Dates")# Display the pie chartpie(x, label = mylabel, main = "Fruits")
结果如下:

颜色
您可以使用 col 参数为每个饼图添加颜色:
实例
bitmap(file="out.png")# Create a vector of piesx <- c(10,20,30,40)# Create a vector of labelsmylabel <- c("Apples", "Bananas", "Cherries", "Dates")# Create a vector of colorscolors <- c("blue", "yellow", "green", "black")# Display the pie chartpie(x, label = mylabel, main = "Fruits", col = colors)
结果如下:

图例
要为每个饼图添加解释列表,请使用 legend() 函数:
实例
bitmap(file="out.png")# Create a vector of piesx <- c(10,20,30,40)# Create a vector of labelsmylabel <- c("Apples", "Bananas", "Cherries", "Dates")# Create a vector of colorscolors <- c("blue", "yellow", "green", "black")# Display the pie chartpie(x, label = mylabel, main = "Pie Chart", col = colors)# Display the explanation boxlegend("bottomright", mylabel, fill = colors)
结果如下:

图例可以定位为:
bottomright, bottom, bottomleft, left, topleft, top, topright, right, center