Skip to content

Latest commit

 

History

History
40 lines (38 loc) · 985 Bytes

File metadata and controls

40 lines (38 loc) · 985 Bytes

Pie Chart for Proportion

Base R Graphic

Preparation Code
# Functions
library(glue)

# Sample Data
library(dplyr)
HEC <- HairEyeColor %>% as.data.frame()
HEC <- HEC[c("Hair", "Freq")]
HEC <- aggregate(Freq~Hair, HEC, sum)
Actual Code
pie(HEC$Freq,
    labels = glue::glue("{HEC$Hair}, {round(100 * HEC$Freq/sum(HEC$Freq),2)}%"),
    main = "Pie Chart of Frequency of Hair Colour",
    cex.main = 0.9,
    col = c("plum2", "lightgoldenrod1", "pink", "lightsteelblue1"))

ggplot2 Graphic

Preparation Code
# Functions
library(ggplot2)

# Sample Data
library(dplyr)
HEC <- HairEyeColor %>% as.data.frame()
HEC <- HEC[c("Hair", "Freq")]
HEC <- aggregate(Freq~Hair, HEC, sum)
Actual Code
ggplot(HEC, aes(x = "", y = Freq, fill = Hair)) +
        geom_bar(width = 1, stat = "identity") +
        coord_polar("y", start = 0) +
        scale_fill_manual(values = c("plum2", "lightgoldenrod1", "pink", "lightsteelblue1"))