R 简明教程

R - Pie Charts

R 编程语言有许多用于创建图表和图形的库。饼图是将值表示为带有不同颜色的圆形的切片的表示形式。这些切片带标签,并且每个切片对应的数字也显示在图表中。

R Programming language has numerous libraries to create charts and graphs. A pie-chart is a representation of values as slices of a circle with different colors. The slices are labeled and the numbers corresponding to each slice is also represented in the chart.

在 R 中,使用 pie() 函数创建饼图,该函数接受正数作为向量输入。其他参数用于控制标签、颜色、标题等。

In R the pie chart is created using the pie() function which takes positive numbers as a vector input. The additional parameters are used to control labels, color, title etc.

Syntax

使用 R 创建饼图的基本语法为:

The basic syntax for creating a pie-chart using the R is −

pie(x, labels, radius, main, col, clockwise)

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. x is a vector containing the numeric values used in the pie chart.

  2. labels is used to give description to the slices.

  3. radius indicates the radius of the circle of the pie chart.(value between −1 and +1).

  4. main indicates the title of the chart.

  5. col indicates the color palette.

  6. clockwise is a logical value indicating if the slices are drawn clockwise or anti clockwise.

Example

仅使用输入矢量和标签就可以创建一个非常简单的饼状图。以下脚本将在当前 R 工作目录中创建和保存饼状图。

A very simple pie-chart is created using just the input vector and labels. The below script will create and save the pie chart in the current R working directory.

# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")

# Give the chart file a name.
png(file = "city.png")

# Plot the chart.
pie(x,labels)

# Save the file.
dev.off()

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

city

Pie Chart Title and Colors

我们可以通过向函数添加更多参数来扩展图表的功能。我们将使用参数 main 为图表添加标题,另一个参数是 col ,它将在绘制图表时使用彩虹色板。色板的长度应与图表的值的数量相同。因此,我们使用 length(x)。

We can expand the features of the chart by adding more parameters to the function. We will use parameter main to add a title to the chart and another parameter is col which will make use of rainbow colour pallet while drawing the chart. The length of the pallet should be same as the number of values we have for the chart. Hence we use length(x).

Example

以下脚本将在当前 R 工作目录中创建和保存饼状图。

The below script will create and save the pie chart in the current R working directory.

# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")

# Give the chart file a name.
png(file = "city_title_colours.jpg")

# Plot the chart with title and rainbow color pallet.
pie(x, labels, main = "City pie chart", col = rainbow(length(x)))

# Save the file.
dev.off()

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

city title colours

Slice Percentages and Chart Legend

我们可以通过创建其他图表变量来添加切片百分比和图表图例。

We can add slice percentage and a chart legend by creating additional chart variables.

# Create data for the graph.
x <-  c(21, 62, 10,53)
labels <-  c("London","New York","Singapore","Mumbai")

piepercent<- round(100*x/sum(x), 1)

# Give the chart file a name.
png(file = "city_percentage_legends.jpg")

# Plot the chart.
pie(x, labels = piepercent, main = "City pie chart",col = rainbow(length(x)))
legend("topright", c("London","New York","Singapore","Mumbai"), cex = 0.8,
   fill = rainbow(length(x)))

# Save the file.
dev.off()

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

city percentage legends

3D Pie Chart

可以使用其他包来绘制三维饼状图。包 plotrix 有一个函数 pie3D() ,用于此目的。

A pie chart with 3 dimensions can be drawn using additional packages. The package plotrix has a function called pie3D() that is used for this.

# Get the library.
library(plotrix)

# Create data for the graph.
x <-  c(21, 62, 10,53)
lbl <-  c("London","New York","Singapore","Mumbai")

# Give the chart file a name.
png(file = "3d_pie_chart.jpg")

# Plot the chart.
pie3D(x,labels = lbl,explode = 0.1, main = "Pie Chart of Countries ")

# Save the file.
dev.off()

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

3d pie chart