R Pie Charts


Pie Charts

Pie chart is a circular view of the data.

Use pie() function to draw pie charts


Example
# Create a vector of pies
x <- c(10,20,30,40)

# Display the pie chart
pie(x)

Result

Example Explained

As you can see, the chart there pulls one pie of each value per vector (in this case 10, 20, 30, 40).

By default, the order of the first pie starts at x-axis and goes counterclockwise.


Note: The size of each pie is determined by comparing the value with all other values, using this formula:

Division divided by the sum of all values: x / sum (x)



Start Angle

You can change the first angle of the pie chart with the init.angle parameter.

The init.angle value is defined by an angle in degrees, where the default angle is 0.


Example

Start the first pie at 90 degrees:

# Create a vector of pies
x <- c(10,20,30,40)

# Display the pie chart and start the first pie at 90 degrees
pie(x, init.angle = 90)

Result


Labels and Header

Use the label parameter to add the label to the pie chart, and use the main parameter to add a title:


Example
# Create a vector of pies
x <- c(10,20,30,40)

# Create a vector of labels
mylabel <- c("Apples", "Bananas", "Cherries", "Dates")

# Display the pie chart with labels
pie(x, label = mylabel, main = "Fruits")

Result


Colors

You can add color to each pie with col parameter:


Example
# Create a vector of colors
colors <- c("blue", "yellow", "green", "black")

# Display the pie chart with colors
pie(x, label = mylabel, main = "Fruits", col = colors)

Result


Legend

To add a description list for each pie, use the legend() function:


Example
# Create a vector of labels
mylabel <- c("Apples", "Bananas", "Cherries", "Dates")

# Create a vector of colors
colors <- c("blue", "yellow", "green", "black")

# Display the pie chart with colors
pie(x, label = mylabel, main = "Pie Chart", col = colors)

# Display the explanation box
legend("bottomright", mylabel, fill = colors)

Result

The myth may be as follows:

bottomright, bottom, bottomleft, left, topleft, top, topright, right, center