# Extract data for figure 1: (column 30)
fig1_raw <- data %>%
select(30) %>%
rename(favourite_drink = "What is your favorite coffee drink?") %>%
drop_na()
# Create summary count by coffee drink
fig1_final <- fig1_raw %>%
group_by(favourite_drink) %>%
summarise(count=n()) %>%
mutate(percentage = round(count/sum(count)*100))
# Rename one drink for label-purposes
fig1_final[2,1] <- "Blended drink"
p1 <- ggplot(fig1_final,
aes(fill = favourite_drink, area = percentage, label =
paste0(favourite_drink, "\n", percentage))) +
geom_treemap() +
geom_treemap_text(colour = "black", place = "centre", reflow = T, min.size=0, size = 10) +
labs(title = "Pourover is the favourite coffee drink among respondents",
subtitle = "27% favour pourover, followed by 17% favouring Latte") +
scale_fill_brewer(palette="Paired") +
theme(legend.position = "none")
p1
# Extract data for figure 2: (columns 5 to 9)
fig2_raw <- data %>%
select(5,6,7,8,9) %>%
rename(at_home = "Where do you typically drink coffee? (At home)",
at_office = "Where do you typically drink coffee? (At the office)",
on_the_go = "Where do you typically drink coffee? (On the go)",
at_cafe="Where do you typically drink coffee? (At a cafe)",
other = "Where do you typically drink coffee? (None of these)") %>%
drop_na()
# Create summary count for TRUE values only
# Source: https://stackoverflow.com/questions/66639523/how-to-build-a-summary-table-with-different-counts-on-col?rq=3
fig2_final <- setNames(rev(stack(colSums(fig2_raw))),c("venue","count")) %>%
mutate(percentage = round(count/nrow(fig2_raw)*100))
p2 <- fig2_final %>%
arrange(percentage) %>%
mutate(venue = factor(venue, levels=venue)) %>%
ggplot(aes(x= venue, y = percentage)) +
geom_bar(stat = "identity", fill = "#FFB14E") +
scale_x_discrete(labels=c('Elsewhere', 'On the go', 'At a cafe', 'At the office', 'At home')) +
coord_flip() +
labs(title = "Over 90% of respondents drink coffee at home",
subtitle = "Multiple responses were possible") +
ylab("Percentage") +
theme_minimal() +
theme(axis.title.y = element_blank()) +
geom_text(aes(label = percentage), size = 3, hjust = -0.5)
p2
# Change logical variable type (TRUE, FALSE) to integer type (0,1) to be able to apply filter function
fig3_raw <- rename(data, home_brewing = "Where do you typically drink coffee? (At home)")
fig3_raw$home_brewing <- as.integer(as.logical(fig3_raw$home_brewing))
fig3_raw <- fig3_raw %>%
filter(home_brewing == 1)
# Extract data for figure 3: columns 11 to 20
fig3_raw <-fig3_raw %>%
select(11,12,13,14,15,16,17,18,19,20) %>%
rename(pour_over = "How do you brew coffee at home? (Pour over)",
french_press = "How do you brew coffee at home? (French press)",
espresso = "How do you brew coffee at home? (Espresso)",
brewing_machine = "How do you brew coffee at home? (Coffee brewing machine (e.g. Mr. Coffee))",
capsule_machine = "How do you brew coffee at home? (Pod/capsule machine (e.g. Keurig/Nespresso))",
instant_coffee ="How do you brew coffee at home? (Instant coffee)",
bean_to_cup_machine = "How do you brew coffee at home? (Bean-to-cup machine)",
cold_brew = "How do you brew coffee at home? (Cold brew)",
coffee_extract = "How do you brew coffee at home? (Coffee extract (e.g. Cometeer))",
other = "How do you brew coffee at home? (Other)") %>%
drop_na()
# Create summary count for TRUE values only, source: https://stackoverflow.com/questions/66639523/how-to-build-a-summary-table-with-different-counts-on-col?rq=3
fig3_final <- setNames(rev(stack(colSums(fig3_raw))),c("method","count")) %>%
mutate(percentage = round(count/nrow(fig3_raw)*100))
p3 <- fig3_final %>%
arrange(percentage) %>%
mutate(method = factor(method, levels=method)) %>%
ggplot(aes(x= method, y = percentage)) +
geom_bar(stat = "identity", fill = "#FFB14E") +
scale_x_discrete(labels=c('Bean-to-cup machine', 'Instant coffee', 'Coffee extract', 'Capsule machine', 'Cold brew', 'Brewing machine', 'Other', 'French press', 'Espresso', 'Pour over')) +
coord_flip() +
labs(title = "Over 60% of people drinking coffee at home pour over ",
subtitle = "Multiple responses were possible") +
ylab("Percentage") +
theme_minimal() +
theme(axis.title.y = element_blank()) +
geom_text(aes(label = percentage), size = 3, hjust = -0.5)
p3
# Extract data for figure 4
fig4_raw <- data %>%
select("What is your age?",
"How many cups of coffee do you typically drink per day?",
"Lastly, how would you rate your own coffee expertise?") %>%
rename(age = "What is your age?",
coffee_cups = "How many cups of coffee do you typically drink per day?",
coffee_expertise = "Lastly, how would you rate your own coffee expertise?") %>%
drop_na()
# Create summary count
fig4_final <- fig4_raw %>%
group_by(age, coffee_cups) %>%
summarise(coffee_expertise_mean=mean(coffee_expertise))
## `summarise()` has grouped output by 'age'. You can override using the `.groups`
## argument.
# Create plot
p4 <- ggplot(fig4_final, aes(age, coffee_cups, fill=coffee_expertise_mean)) +
geom_tile() +
scale_fill_distiller(palette = "Blues", limits =c(0,10), direction = 1) +
theme_ipsum() +
theme(axis.title.x = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
axis.title.y = element_text(hjust = 0.5),
plot.title = element_text(face = "plain")) +
ylab("Coffee cups per day") +
labs(title = "Average self-evaluated coffee expertise\nper age group and daily coffee consumption",
subtitle = "Level of coffee expertise ranging from low (1) to high (10)") +
guides(fill=guide_colourbar(title="Coffee expertise (mean)"))
p4_animated <- ggplotly(p4)
p4_animated
# Extract data for figure 5
fig5_raw <- data %>%
select("Before today's tasting, which of the following best described what kind of coffee you like?") %>%
rename(taste = "Before today's tasting, which of the following best described what kind of coffee you like?") %>%
drop_na()
# Create summary count
fig5_final <- fig5_raw %>%
group_by(taste) %>%
summarise(count = n()) %>%
mutate(percentage = round(count/nrow(fig5_raw)*100))
# Create plot
packing <- circleProgressiveLayout(fig5_final$percentage, sizetype='area')
## Warning in circleProgressiveLayout(fig5_final$percentage, sizetype = "area"):
## missing and/or non-positive sizes will be ignored
fig5_final <- cbind(fig5_final, packing)
fig5_final.gg <- circleLayoutVertices(packing, npoints=50)
p5 <- ggplot() +
# Make the bubbles
geom_polygon(data = fig5_final.gg, aes(x, y, taste = id, fill=as.factor(id)), colour = "black", alpha = 0.6) +
# Add text in the center of each bubble + control its size
geom_text(data = fig5_final, aes(x, y, size=percentage, label = paste0(taste, '\n', scales::comma(percentage)))) +
scale_size_continuous(range = c(1,4)) +
# General theme:
theme_void() +
theme(legend.position="none") +
coord_equal() +
labs(title = "24% of respondents prefer fruity coffee",
subtitle = "Second comes chocolately coffee, favoured by 16% of respondents")
## Warning in geom_polygon(data = fig5_final.gg, aes(x, y, taste = id, fill =
## as.factor(id)), : Ignoring unknown aesthetics: taste
p5
## Warning: Removed 2 rows containing missing values (`geom_text()`).
# Extract data for figure 6
fig6_raw <- data %>%
filter(Gender == "Female" | Gender == "Male") %>%
select("Before today's tasting, which of the following best described what kind of coffee you like?",
"Gender") %>%
rename(taste = "Before today's tasting, which of the following best described what kind of coffee you like?",
gender = "Gender") %>%
drop_na()
# Create summary count and percentages for each gender, source: https://stackoverflow.com/questions/29549731/finding-percentage-in-a-sub-group-using-group-by-and-summarise
fig6_final <- fig6_raw %>%
group_by(gender, taste) %>%
summarise(count = n())
## `summarise()` has grouped output by 'gender'. You can override using the
## `.groups` argument.
fig6_final <- fig6_final %>%
group_by(gender) %>%
mutate(countT= sum(count)) %>%
group_by(taste, add=TRUE) %>%
mutate(percentage=paste0(round(100*count/countT,0)))
## Warning: The `add` argument of `group_by()` is deprecated as of dplyr 1.0.0.
## ℹ Please use the `.add` argument instead.
## ℹ The deprecated feature was likely used in the dplyr package.
## Please report the issue at <https://github.com/tidyverse/dplyr/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
fig6_final$count <- NULL
fig6_final$countT <- NULL
fig6_final <- rename(fig6_final, "Gender"=gender) #correct labelling of legend
# Create plot
p6 <- ggplot(fig6_final, aes(x = as.integer(percentage), y = taste)) +
geom_line() +
geom_point(aes(color = Gender), size = 7) +
scale_color_manual(values=c("darkred","darkblue")) +
geom_text(aes(label=percentage), colour = "white", size = 3, show.legend=FALSE) +
labs(title = "Males favour fruity coffee and females favour chocolatey coffee",
subtitle = "Percentages of respondents favouring respective coffee taste",
x = "Percentage",
y ="") +
theme(legend.title=element_blank()) +
theme_minimal()
p6
# Extract data for figure 7
fig7_raw <- data %>%
select("How much caffeine do you like in your coffee?") %>%
rename(coffeine = "How much caffeine do you like in your coffee?") %>%
drop_na()
# Create summary count
fig7_final <- fig7_raw %>%
group_by(coffeine) %>%
summarise(count = n()) %>%
mutate(percentage = round(count/nrow(fig7_raw)*100))
# correction to make all percentages add up to 100%
correction <- c(0,1,0)
fig7_final <- fig7_final %>%
mutate(percentage = percentage + correction)
# sort by percentage
fig7_final <- fig7_final[order(fig7_final$percentage, decreasing = TRUE), ]
# Create plot
cap <- paste0("1 square = 1 %")
p7 <- ggplot(fig7_final, aes(fill = coffeine, values=percentage)) +
geom_waffle(na.rm=TRUE,
n_rows = 10,
size = .4,
color = "white") +
scale_fill_manual(values = c("tan", "tan4", "tan3")) +
coord_equal() +
theme_minimal() +
theme_enhance_waffle() +
theme(legend.title = element_blank()) +
labs(title = "92% of respondents prefer coffee with caffeine",
caption = cap)
p7
# Extract data for figure 8
fig8_raw <- data %>%
select("In total, much money do you typically spend on coffee in a month?",
"Gender") %>%
rename(coffee_budget = "In total, much money do you typically spend on coffee in a month?",
gender = "Gender") %>%
filter(gender == "Female" | gender == "Male") %>%
drop_na()
# Create summary count
fig8_final <- fig8_raw %>%
group_by(coffee_budget, gender) %>%
summarise(count = n()) %>%
group_by(gender) %>%
mutate(countT= sum(count)) %>%
group_by(coffee_budget, add=TRUE) %>%
mutate(percentage=paste0(round(100*count/countT,0)))
## `summarise()` has grouped output by 'coffee_budget'. You can override using the
## `.groups` argument.
# change formatting of coffee_budget variable to change x-axis order of bar plot
fig8_final <- fig8_final %>%
mutate(coffee_budget = factor(coffee_budget, levels=c("<$20","$20-$40","$40-$60","$60-$80","$80-$100",">$100")))
# Rename variable for legend
fig8_final <- rename(fig8_final, "Gender" = gender)
# Create plot
p8 <- ggplot(fig8_final, aes(fill=Gender, x=coffee_budget, y=as.integer(percentage))) +
geom_bar(position="dodge", stat="identity") +
scale_fill_manual(values=c("darkred", "darkblue")) +
geom_text(aes(label = percentage), position = position_dodge(.9), colour ="white", vjust=1.5) +
labs(title ="The majority of females and males spend between $20-$60\nper month on coffee") +
xlab("Monthly amount spent on coffee") +
ylab("Percentage") +
theme_minimal()
p8