Pie Charts
A pie chart is a circular graphical view of data.
Use the
function to draw pie charts:pie(>
Example
x <- c(10,20,30,40>
# Display
the pie chart
pie(x>
Example Explained
As you can see the pie chart draws one pie for each value in the vector (in this case
10, 20, 30, 40>
.
By default, the plotting of the first pie starts from the x-axis and move counterclockwise.
The value divided by the sum of all values: x/sum(x>
Labels and Header
Use the
parameter to add a label to the pie chart,
and use the label
parameter to add a header:main
Example
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">
Colors
You can add a color to each pie with the
parameter:col
Example
colors <- c("blue", "yellow", "green", "black">
# Display the pie chart
with colors
pie(x, label = mylabel, main =
"Fruits", col = colors>
Legend
To add a list of explanation for each pie, use the
function:legend(>
Example
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>
bottomright,
bottom,
bottomleft,
left,
topleft,
top,
topright,
right,
center