-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpie-bar-chart.R
59 lines (45 loc) · 1.88 KB
/
pie-bar-chart.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
library(ggplot2)
# Set the seed for reproducibility
set.seed(123)
# Generate random data
categories <- c("Beer", "Wine", "Liquor - Mixed", "Liquor - Straight", "Non-alcoholic")
percentages <- c(1, .8, .75, .4, .2)
s <- sum(percentages)
percentages <- percentages / s
# Create a data frame
data <- data.frame(categories, percentages)
# Sort the data frame by descending order of proportions
data <- data[order(data$percentages, decreasing = TRUE), ]
library(RColorBrewer)
cp <- brewer.pal(length(categories), "Dark2")
# Create a pie chart using ggplot2
chart <- ggplot(data, aes(x = "", y = percentages, fill = categories)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
labs(fill = "Categories", x = NULL, y = NULL, title = "What do people order at the bar?") +
theme_void() +
theme(legend.position = "bottom") +
scale_fill_manual(values = cp) +
geom_text(aes(label = paste0(round(percentages * 100), "%")),
position = position_stack(vjust = 0.5)) # Add data labels
chart
# Generate random data
categories <- c("Apple", "Peach", "Pumpkin", "Pecan", "Key Lime", "Other")
percentages <- c(1, .8, .75, .4, .6, .3)
s <- sum(percentages)
percentages <- percentages / s
# Create a data frame
data <- data.frame(categories, percentages)
# Sort the data frame by descending order of proportions
data <- data[order(data$percentages, decreasing = TRUE), ]
library(RColorBrewer)
cp <- brewer.pal(length(categories), "Dark2")
# Create a pie chart using ggplot2
chart <- ggplot(data, aes(x = categories, y = percentages)) +
geom_bar(stat = "identity", fill = "steelblue") +
scale_y_continuous(labels = scales::percent_format()) +
labs(x = "Type of Pie", y = "Percentage of People", title = "Favorite Pie") +
theme_minimal() +
theme(legend.position = "bottom") +
scale_fill_manual(values = cp)
chart