# Load required libraries
pacman::p_load(pacman, readr, ggplot2, lubridate, scales, dplyr, gridExtra)
# Read the data
DT <- read_csv("DT.csv")
## Rows: 13 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (1): Year
## time (1): Daily Time (Hours:Minutes)
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
SM <- read_csv("SM.csv")
## Rows: 52 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Country
## time (1): Daily Time (Hours:Minutes)
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Convert 'Daily Time (Hours:Minutes)' to a time period (hms) object in both datasets
DT <- DT %>%
mutate(Daily_Time_hms = hms::as_hms(`Daily Time (Hours:Minutes)`))
SM <- SM %>%
mutate(Daily_Time_hms = hms::as_hms(`Daily Time (Hours:Minutes)`)) %>%
arrange(Daily_Time_hms)
# Select top 10 and bottom 10 countries based on daily time in SM data
top_10 <- SM %>%
top_n(10, Daily_Time_hms) %>%
mutate(Country = factor(Country, levels = Country[order(-Daily_Time_hms)])) # Descending order for top 10
bottom_10 <- SM %>%
slice_head(n = 10) %>%
mutate(Country = factor(Country, levels = Country[order(Daily_Time_hms)])) # Ascending order for bottom 10
# Function to format hours and minutes for labels
format_hms <- function(time) {
paste(hour(time), ":", sprintf("%02d", minute(time)), sep = "")
}
# Function to create a pie chart with labels and custom legend order
create_pie_chart <- function(data, title) {
ggplot(data, aes(x = "", y = Daily_Time_hms, fill = Country)) +
geom_bar(width = 1, stat = "identity", color = "#ffffff") +
coord_polar("y", start = 0) +
scale_fill_brewer(palette = "Paired") +
labs(title = title, fill = "Country") +
geom_text(
aes(label = format_hms(Daily_Time_hms)),
position = position_stack(vjust = 0.5), color = "#000000", size = 4, fontface = "bold") + # Make the labels bold
theme_void() +
theme(plot.title = element_text(hjust = 0.5))
}
# Create the top 10 and bottom 10 pie chart data
pie_top_10 <- create_pie_chart(top_10, "Top 10 Countries by avg Daily Time Spent on Social Media (Q1 2023; 16-64yrs; hr:min)")
pie_bottom_10 <- create_pie_chart(bottom_10, "Bottom 10 Countries by avg Daily Time Spent on Social Media (Q1 2023; 16-64yrs; hr:min)")
# Line chart for Average Daily Time Spent on Social Media Worldwide with a trend line
p2 <- ggplot(DT, aes(x = Year, y = Daily_Time_hms)) +
geom_line(color = "#000000", linewidth = 1) + # Line for the data points
geom_point(color = "#000000", size = 3) + # Points on the line
geom_smooth(method = "lm", se = FALSE, color = "#ff0000", linetype = "dashed", linewidth = 1) + # Add a linear trend line
scale_x_continuous(breaks = DT$Year) + # Ensure each year is shown on the x-axis
scale_y_time(labels = scales::time_format("%H:%M")) + # Format y-axis as HH:MM
labs(
title = "Average Daily Time Spent on Social Media Worldwide (2012-2024; 16-64yrs; hr:min)",
x = "Year",
y = "Daily Time (Hours:Minutes)"
) +
theme_minimal()
# Bar chart for average daily time by country in descending order with data labels
p1 <- ggplot(SM, aes(x = reorder(Country, -Daily_Time_hms), y = Daily_Time_hms)) + # Reorder countries in descending order
geom_bar(stat = "identity", fill = "#000000") +
geom_text(
aes(label = scales::time_format("%H:%M")(Daily_Time_hms)),
color = "#ffffff", size = 3, fontface = "bold", angle = 90, hjust = 1.2, vjust = 0.5
) + # Rotate labels, place inside end of bars, in white
scale_y_time(labels = scales::time_format("%H:%M")) + # Format y-axis as HH:MM
labs(
title = "Average Daily Time Spent on Social Media by Country (Q1 2023; 16-64yrs; hr:min)",
x = "Country",
y = "Average Daily Time (Hours:Minutes)"
) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 60, hjust = 1)) # Rotate x-axis labels
# Arrange the plots
grid.arrange(pie_top_10, pie_bottom_10, p1, p2, ncol = 2)
## `geom_smooth()` using formula = 'y ~ x'
