R pie() function - Plotting Pie Charts

R 语言实例R Language Examples

R pie() function is used to draw pie charts, showing the proportion of each part in the whole.

Pie charts are suitable for showing proportions of no more than 5-6 categories.

The syntax of the pie() function is as follows:

pie(x, labels = names(x), radius = 0.8,
    main = "", col = NULL, clockwise = FALSE)

Parameter description:

  • xA vector of values for each part.

  • labelsLabels for each part.

  • radiusPie chart radius (0-1), default is 0.8.

Example

# Market share data
market_share <- c(35, 28, 20, 12, 5)
names(market_share) <- c("Company A", "Company B", "Company C",
                          "Company D", "Others")

# Calculate percentage
pct <- round(market_share / sum(market_share) * 100, 1)
labels <- paste(names(market_share), "\n", pct, "%", sep = "")

# Draw pie chart
pie(market_share,
    labels = labels,
    main = "Market Share Distribution",
    col = c("steelblue", "coral", "seagreen",
            "gold", "gray"))

Running the above code will display a pie chart of each company's market share with percentage labels.

R 语言实例R Language Examples

Other Extensions