In the following code hunk, import your data.
#### Use read_csv() or another function
#### Make sure your data is converted into a tibble.
#### For demonstration purposes, this example uses the mtcars data.
library(ggplot2)
library(tidyr)
library(dplyr)
library(readxl)
library(gganimate)
library(stringr)
# Import the data
data <- read_excel("C:/Users/USER/Downloads/未命名的試算表.xlsx")
## Part 1
# Filtering and reshaping data
data_long <- data %>%
pivot_longer(cols = starts_with("2022") | starts_with("2023") | starts_with("2024"),
names_to = "Quarter",
values_to = "Sales_Revenue_Million_KRW") %>%
filter(`(Million KRW)` == "Sales Revenue")
# Ensure the 'Quarter' column is in the correct format
data_long <- data_long %>%
mutate(Quarter = factor(Quarter, levels = c("2022.Q1", "2022.Q2", "2022.Q3", "2022.Q4",
"2023.Q1", "2023.Q2", "2023.Q3", "2023.Q4",
"2024.Q1", "2024.Q2", "2024.Q3", "2024.Q4")))
# Check the contents of data_long
print(data_long)
## # A tibble: 9 × 3
## `(Million KRW)` Quarter Sales_Revenue_Million_KRW
## <chr> <fct> <dbl>
## 1 Sales Revenue 2022.Q1 284974
## 2 Sales Revenue 2022.Q2 512218
## 3 Sales Revenue 2022.Q3 445499
## 4 Sales Revenue 2022.Q4 533463
## 5 Sales Revenue 2023.Q1 410635
## 6 Sales Revenue 2023.Q2 620990
## 7 Sales Revenue 2023.Q3 537859
## 8 Sales Revenue 2023.Q4 608604
## 9 Sales Revenue 2024.Q1 360918
# Ensure Sales_Revenue_Million_KRW is numeric
data_long <- data_long %>%
mutate(Sales_Revenue_Million_KRW = as.numeric(Sales_Revenue_Million_KRW))
# Calculate the maximum value for y-axis limit
max_value <- max(data_long$Sales_Revenue_Million_KRW, na.rm = TRUE)
# Identify the quarter with the maximum sales revenue
max_quarter <- data_long %>%
filter(Sales_Revenue_Million_KRW == max_value) %>%
pull(Quarter)
# Plot the histogram with the customized fill legend
plot <- ggplot(data_long, aes(x = Quarter, y = Sales_Revenue_Million_KRW, fill = Quarter == max_quarter)) +
geom_col(width = 0.7) +
scale_fill_manual(values = c("TRUE" = 'red', "FALSE" = 'blue'),
labels = c("TRUE" = "The biggest", "FALSE" = "General")) +
labs(title = 'Sales Revenue per Quarter',
x = 'Quarter',
y = 'Sales Revenue (Million KRW)',
fill = 'Category') +
theme_minimal() +
scale_y_continuous(limits = c(0, max_value * 1.1))
# Add animation to present columns one by one
animated_plot <- plot +
transition_states(Quarter, transition_length = 2, state_length = 1) +
enter_fade() +
exit_fade() +
ease_aes('linear')
# Render the animation
animate(animated_plot, nframes = length(unique(data_long$Quarter)) * 10, fps = 5)
# Save the animation as a gif
anim_save("sales_revenue_per_quarter.gif", animation = last_animation())
Using words, describe the second visualization you are going to make using which variables/characteristics in your data:
Example: For my second figure, I am going to create a line chart that indicates the profits before tax of each quarter of 2022 and 2023.
In the code chunk below, show your work filtering the data and create the subset of data you will display graphically.
data <- read_excel("C:/Users/USER/Downloads/未命名的試算表.xlsx")
profit_2022 <- data %>%
select(starts_with("2022")) %>%
summarise_all(sum, na.rm = TRUE) %>%
pivot_longer(everything(), names_to = "Quarter", values_to = "Profit_before_tax") %>%
mutate(Year = "2022")
# Extract profit before tax data for 2023
profit_2023 <- data %>%
select(starts_with("2023")) %>%
summarise_all(sum, na.rm = TRUE) %>%
pivot_longer(everything(), names_to = "Quarter", values_to = "Profit_before_tax") %>%
mutate(Year = "2023")
# Combine profit data for 2022 and 2023
combined_profit <- bind_rows(profit_2022, profit_2023)
# Reorder the quarters
combined_profit$Quarter <- factor(combined_profit$Quarter, levels = c("2022.Q1", "2022.Q2", "2022.Q3", "2022.Q4", "2023.Q1", "2023.Q2", "2023.Q3", "2023.Q4"),
labels = c("Q1", "Q2", "Q3", "Q4", "Q1", "Q2", "Q3", "Q4"))
# Plot the line chart
ggplot(combined_profit, aes(x = Quarter, y = Profit_before_tax, color = Year, group = Year)) +
geom_line() +
labs(x = "Quarter", y = "Profit before tax", color = "Year") +
ggtitle("Profit before tax comparison between 2022 and 2023")
## Part 3
Using words, describe the third visualization you are going to make using which variables/characteristics in your data:
Example: For the third figure, I will display a scatter plot of the music records and total sales Revenue in 2022 and 2023
data <- read_excel("C:/Users/USER/Downloads/未命名的試算表.xlsx")
print(names(data))
## [1] "(Million KRW)" "2022.Q1" "2022.Q2" "2022.Q3"
## [5] "2022.Q4" "2023.Q1" "2023.Q2" "2023.Q3"
## [9] "2023.Q4" "2024.Q1"
str(data)
## tibble [27 × 10] (S3: tbl_df/tbl/data.frame)
## $ (Million KRW): chr [1:27] "Sales Revenue" "Music Records" "Performances" "Advertising and Appearance Fees" ...
## $ 2022.Q1 : num [1:27] 284974 64643 61306 26374 69565 ...
## $ 2022.Q2 : num [1:27] 512218 210890 84959 30033 98783 ...
## $ 2022.Q3 : num [1:27] 445499 129198 47232 29805 114717 ...
## $ 2022.Q4 : num [1:27] 533463 147257 64671 75618 112489 ...
## $ 2023.Q1 : num [1:27] 410635 184288 25229 24973 68920 ...
## $ 2023.Q2 : num [1:27] 620990 245890 157512 33044 111907 ...
## $ 2023.Q3 : num [1:27] 537859 264126 86873 31418 85658 ...
## $ 2023.Q4 : num [1:27] 608604 276159 89497 52464 59077 ...
## $ 2024.Q1 : num [1:27] 360918 145127 44036 27818 60747 ...
sales_revenue <- data %>% filter(`(Million KRW)` == "Sales Revenue")
music_records <- data %>% filter(`(Million KRW)` == "Music Records")
# Reshape the data to long format
sales_revenue_long <- sales_revenue %>%
pivot_longer(cols = starts_with("202"), names_to = "Quarter", values_to = "Sales_Revenue") %>%
select(-`(Million KRW)`)
music_records_long <- music_records %>%
pivot_longer(cols = starts_with("202"), names_to = "Quarter", values_to = "Music_Records") %>%
select(-`(Million KRW)`)
# Combine the two data frames
combined_data <- inner_join(sales_revenue_long, music_records_long, by = "Quarter")
# Extract the year from the Quarter column
combined_data <- combined_data %>%
mutate(Year = ifelse(str_detect(Quarter, "^2022"), "2022", "2023"))
# Plot the scatter plot with trendlines
ggplot(combined_data, aes(x = Music_Records, y = Sales_Revenue, color = Year)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
scale_color_manual(values = c("2022" = "red", "2023" = "blue")) +
labs(x = "Music Records", y = "Sales Revenue", color = "Year") +
ggtitle("Scatter Plot of Music Records vs. Sales Revenue with Trendlines")
## `geom_smooth()` using formula = 'y ~ x'
## Part 4 Using words, describe the fourth visualization you
are going to make using which variables/characteristics in your
data:
Example: For my second figure, I am going to create a line chart that compares the Operating Profit Margin (%)between 2022 and 2023
In the code chunk below, show your work filtering the data and create the subset of data you will display graphically.
# 读取数据
data <- read_excel("C:/Users/USER/Downloads/未命名的試算表.xlsx")
# 提取2022年的利润数据
profit_2022 <- data %>%
select(starts_with("2022")) %>%
summarise_all(sum, na.rm = TRUE) %>%
pivot_longer(everything(), names_to = "Quarter", values_to = "Operating_Profit_Margin") %>%
mutate(Year = "2022")
# 提取2023年的利润数据
profit_2023 <- data %>%
select(starts_with("2023")) %>%
summarise_all(sum, na.rm = TRUE) %>%
pivot_longer(everything(), names_to = "Quarter", values_to = "Operating_Profit_Margin") %>%
mutate(Year = "2023")
# 合并2022年和2023年的利润数据
combined_profit <- bind_rows(profit_2022, profit_2023)
# 重命名季度
combined_profit$Quarter <- factor(combined_profit$Quarter,
levels = c("2022.Q1", "2022.Q2", "2022.Q3", "2022.Q4", "2023.Q1", "2023.Q2", "2023.Q3", "2023.Q4"),
labels = c("2022 Q1", "2022 Q2", "2022 Q3", "2022 Q4", "2023 Q1", "2023 Q2", "2023 Q3", "2023 Q4"))
# 绘制折线图
ggplot(combined_profit, aes(x = Quarter, y = Operating_Profit_Margin, color = Year, group = Year)) +
geom_line() +
labs(x = "Quarter", y = "Operating Profit Margin (%)", color = "Year") +
ggtitle("Operating Profit Margin (%) Comparison between 2022 and 2023")
## Part 5
Using words, describe the fifth visualization you are going to make using which variables/characteristics in your data:
Example: For the third figure, I will display a scatter plot of the music records and total sales Revenue in 2022 and 2023
data <- read_excel("C:/Users/USER/Downloads/未命名的試算表.xlsx")
print(names(data))
## [1] "(Million KRW)" "2022.Q1" "2022.Q2" "2022.Q3"
## [5] "2022.Q4" "2023.Q1" "2023.Q2" "2023.Q3"
## [9] "2023.Q4" "2024.Q1"
str(data)
## tibble [27 × 10] (S3: tbl_df/tbl/data.frame)
## $ (Million KRW): chr [1:27] "Sales Revenue" "Music Records" "Performances" "Advertising and Appearance Fees" ...
## $ 2022.Q1 : num [1:27] 284974 64643 61306 26374 69565 ...
## $ 2022.Q2 : num [1:27] 512218 210890 84959 30033 98783 ...
## $ 2022.Q3 : num [1:27] 445499 129198 47232 29805 114717 ...
## $ 2022.Q4 : num [1:27] 533463 147257 64671 75618 112489 ...
## $ 2023.Q1 : num [1:27] 410635 184288 25229 24973 68920 ...
## $ 2023.Q2 : num [1:27] 620990 245890 157512 33044 111907 ...
## $ 2023.Q3 : num [1:27] 537859 264126 86873 31418 85658 ...
## $ 2023.Q4 : num [1:27] 608604 276159 89497 52464 59077 ...
## $ 2024.Q1 : num [1:27] 360918 145127 44036 27818 60747 ...
sales_revenue <- data %>% filter(`(Million KRW)` == "Sales Revenue")
# 筛选出 'Performances' 数据
performances <- data %>% filter(`(Million KRW)` == "Performances")
# 将数据重塑为长格式
sales_revenue_long <- sales_revenue %>%
pivot_longer(cols = starts_with("202"), names_to = "Quarter", values_to = "Sales_Revenue") %>%
select(-`(Million KRW)`)
performances_long <- performances %>%
pivot_longer(cols = starts_with("202"), names_to = "Quarter", values_to = "Performances") %>%
select(-`(Million KRW)`)
# 合并两个数据框
combined_data <- inner_join(sales_revenue_long, performances_long, by = "Quarter")
# 从Quarter列中提取年份
combined_data <- combined_data %>%
mutate(Year = ifelse(str_detect(Quarter, "^2022"), "2022", "2023"))
# 绘制带有趋势线的散点图
ggplot(combined_data, aes(x = Performances, y = Sales_Revenue, color = Year)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
scale_color_manual(values = c("2022" = "red", "2023" = "blue")) +
labs(x = "Performances Incomes", y = "Sales Revenue", color = "Year") +
ggtitle("Performances Incomes vs. Sales Revenue with Trendlines")
## `geom_smooth()` using formula = 'y ~ x'
## Part 6
Using words, describe the sixth visualization you are going to make using which variables/characteristics in your data:
Example: For the sixth figure, I will display a bar chart of the incomes of each categories in 2022 and 2023
data <- read_excel("C:/Users/USER/Downloads/未命名的試算表.xlsx")
# 檢查數據結構
print(names(data))
## [1] "(Million KRW)" "2022.Q1" "2022.Q2" "2022.Q3"
## [5] "2022.Q4" "2023.Q1" "2023.Q2" "2023.Q3"
## [9] "2023.Q4" "2024.Q1"
str(data)
## tibble [27 × 10] (S3: tbl_df/tbl/data.frame)
## $ (Million KRW): chr [1:27] "Sales Revenue" "Music Records" "Performances" "Advertising and Appearance Fees" ...
## $ 2022.Q1 : num [1:27] 284974 64643 61306 26374 69565 ...
## $ 2022.Q2 : num [1:27] 512218 210890 84959 30033 98783 ...
## $ 2022.Q3 : num [1:27] 445499 129198 47232 29805 114717 ...
## $ 2022.Q4 : num [1:27] 533463 147257 64671 75618 112489 ...
## $ 2023.Q1 : num [1:27] 410635 184288 25229 24973 68920 ...
## $ 2023.Q2 : num [1:27] 620990 245890 157512 33044 111907 ...
## $ 2023.Q3 : num [1:27] 537859 264126 86873 31418 85658 ...
## $ 2023.Q4 : num [1:27] 608604 276159 89497 52464 59077 ...
## $ 2024.Q1 : num [1:27] 360918 145127 44036 27818 60747 ...
# 指定要提取的類別
categories <- c("Sales Revenue", "Music Records", "Performances", "Advertising and Appearance Fees", "MD and Licensing", "contents", "fans club")
# 過濾出感興趣的類別數據
filtered_data <- data %>% filter(`(Million KRW)` %in% categories)
# 檢查是否有缺失的類別
missing_categories <- setdiff(categories, unique(filtered_data$`(Million KRW)`))
if(length(missing_categories) > 0) {
cat("Missing categories in the data:", missing_categories, "\n")
}
# 將數據轉換為長格式
long_data <- filtered_data %>%
pivot_longer(cols = starts_with("202"), names_to = "Quarter", values_to = "Income") %>%
mutate(Year = ifelse(grepl("^2022", Quarter), "2022", "2023"))
# 確保所有類別都包括在內
long_data <- long_data %>%
complete(`(Million KRW)` = categories, Year = c("2022", "2023"), fill = list(Income = 0))
# 檢查長格式數據
print(long_data)
## # A tibble: 63 × 4
## `(Million KRW)` Year Quarter Income
## <chr> <chr> <chr> <dbl>
## 1 Advertising and Appearance Fees 2022 2022.Q1 26374
## 2 Advertising and Appearance Fees 2022 2022.Q2 30033
## 3 Advertising and Appearance Fees 2022 2022.Q3 29805
## 4 Advertising and Appearance Fees 2022 2022.Q4 75618
## 5 Advertising and Appearance Fees 2023 2023.Q1 24973
## 6 Advertising and Appearance Fees 2023 2023.Q2 33044
## 7 Advertising and Appearance Fees 2023 2023.Q3 31418
## 8 Advertising and Appearance Fees 2023 2023.Q4 52464
## 9 Advertising and Appearance Fees 2023 2024.Q1 27818
## 10 contents 2022 2022.Q1 48541
## # ℹ 53 more rows
# 計算每年每個類別的總收入
total_income <- long_data %>%
group_by(`(Million KRW)`, Year) %>%
summarise(Total_Income = sum(Income, na.rm = TRUE)) %>%
ungroup() %>%
rename(Category = `(Million KRW)`)
## `summarise()` has grouped output by '(Million KRW)'. You can override using the
## `.groups` argument.
# 確保所有類別都包括在內
total_income <- total_income %>%
complete(Category = categories, Year = c("2022", "2023"), fill = list(Total_Income = 0))
# 檢查總收入數據
print(total_income)
## # A tibble: 14 × 3
## Category Year Total_Income
## <chr> <chr> <dbl>
## 1 Advertising and Appearance Fees 2022 161830
## 2 Advertising and Appearance Fees 2023 169717
## 3 contents 2022 341500
## 4 contents 2023 351143
## 5 fans club 2022 67113
## 6 fans club 2023 113099
## 7 MD and Licensing 2022 395554
## 8 MD and Licensing 2023 386309
## 9 Music Records 2022 551988
## 10 Music Records 2023 1115590
## 11 Performances 2022 258168
## 12 Performances 2023 403147
## 13 Sales Revenue 2022 1776154
## 14 Sales Revenue 2023 2539006
# 繪製長條圖
ggplot(total_income, aes(x = Category, y = Total_Income, fill = Year)) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Category", y = "Incomes (million KRW)", fill = "Year") +
ggtitle("Incomes Comparison by Category between 2022 and 2023") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
## Part 7
Using words, describe the seventh visualization you are going to make using which variables/characteristics in your data:
Example: For the seventh figure, I will display a box chart of the profits before tax of each quarter in 2022 and 2023
data <- read_excel("C:/Users/USER/Downloads/未命名的試算表.xlsx")
profit_2022 <- data %>%
select(starts_with("2022")) %>%
summarise_all(sum, na.rm = TRUE) %>%
pivot_longer(everything(), names_to = "Quarter", values_to = "Profit_before_tax") %>%
mutate(Year = "2022")
# Extract profit before tax data for 2023
profit_2023 <- data %>%
select(starts_with("2023")) %>%
summarise_all(sum, na.rm = TRUE) %>%
pivot_longer(everything(), names_to = "Quarter", values_to = "Profit_before_tax") %>%
mutate(Year = "2023")
# Combine profit data for 2022 and 2023
combined_profit <- bind_rows(profit_2022, profit_2023)
# Reorder the quarters
combined_profit$Quarter <- factor(combined_profit$Quarter, levels = c("2022.Q1", "2022.Q2", "2022.Q3", "2022.Q4", "2023.Q1", "2023.Q2", "2023.Q3", "2023.Q4"),
labels = c("Q1", "Q2", "Q3", "Q4", "Q1", "Q2", "Q3", "Q4"))
# Plot the line chart
ggplot(combined_profit, aes(x = Quarter, y = Profit_before_tax, color = Year, group = Year)) +
geom_boxplot() +
labs(x = "Quarter", y = "Profit before tax", color = "Year") +
ggtitle("Profit before tax comparison between 2022 and 2023")
## Part 8
Using words, describe the eighth visualization you are going to make using which variables/characteristics in your data:
Example: For the eighth figure, I will display a line chart of the incomes of each categories and profits before tax each quarter in 2022 and 2023
data <- read_excel("C:/Users/USER/Downloads/未命名的試算表.xlsx")
# 檢查數據結構
print(names(data))
## [1] "(Million KRW)" "2022.Q1" "2022.Q2" "2022.Q3"
## [5] "2022.Q4" "2023.Q1" "2023.Q2" "2023.Q3"
## [9] "2023.Q4" "2024.Q1"
str(data)
## tibble [27 × 10] (S3: tbl_df/tbl/data.frame)
## $ (Million KRW): chr [1:27] "Sales Revenue" "Music Records" "Performances" "Advertising and Appearance Fees" ...
## $ 2022.Q1 : num [1:27] 284974 64643 61306 26374 69565 ...
## $ 2022.Q2 : num [1:27] 512218 210890 84959 30033 98783 ...
## $ 2022.Q3 : num [1:27] 445499 129198 47232 29805 114717 ...
## $ 2022.Q4 : num [1:27] 533463 147257 64671 75618 112489 ...
## $ 2023.Q1 : num [1:27] 410635 184288 25229 24973 68920 ...
## $ 2023.Q2 : num [1:27] 620990 245890 157512 33044 111907 ...
## $ 2023.Q3 : num [1:27] 537859 264126 86873 31418 85658 ...
## $ 2023.Q4 : num [1:27] 608604 276159 89497 52464 59077 ...
## $ 2024.Q1 : num [1:27] 360918 145127 44036 27818 60747 ...
# 指定要提取的類別
categories <- c("Sales Revenue", "Music Records", "Performances", "Advertising and Appearance Fees", "MD and Licensing", "contents", "fans club")
# 過濾出感興趣的類別數據
filtered_data <- data %>% filter(`(Million KRW)` %in% categories)
# 檢查是否有缺失的類別
missing_categories <- setdiff(categories, unique(filtered_data$`(Million KRW)`))
if(length(missing_categories) > 0) {
cat("Missing categories in the data:", missing_categories, "\n")
}
# 將數據轉換為長格式
long_data <- filtered_data %>%
pivot_longer(cols = starts_with("202"), names_to = "Quarter", values_to = "Income") %>%
mutate(Year = ifelse(grepl("^2022", Quarter), "2022", "2023"))
# 確保所有類別都包括在內
long_data <- long_data %>%
complete(`(Million KRW)` = categories, Quarter, fill = list(Income = 0))
# 檢查長格式數據
print(long_data)
## # A tibble: 63 × 4
## `(Million KRW)` Quarter Income Year
## <chr> <chr> <dbl> <chr>
## 1 Advertising and Appearance Fees 2022.Q1 26374 2022
## 2 Advertising and Appearance Fees 2022.Q2 30033 2022
## 3 Advertising and Appearance Fees 2022.Q3 29805 2022
## 4 Advertising and Appearance Fees 2022.Q4 75618 2022
## 5 Advertising and Appearance Fees 2023.Q1 24973 2023
## 6 Advertising and Appearance Fees 2023.Q2 33044 2023
## 7 Advertising and Appearance Fees 2023.Q3 31418 2023
## 8 Advertising and Appearance Fees 2023.Q4 52464 2023
## 9 Advertising and Appearance Fees 2024.Q1 27818 2023
## 10 contents 2022.Q1 48541 2022
## # ℹ 53 more rows
# 計算每季度每個類別的 "Profit Before Tax"
# 假設 "Profit Before Tax" 是數據中的 Income
total_income <- long_data %>%
group_by(`(Million KRW)`, Quarter) %>%
summarise(`Profit Before Tax` = sum(Income, na.rm = TRUE)) %>%
ungroup() %>%
rename(Category = `(Million KRW)`)
## `summarise()` has grouped output by '(Million KRW)'. You can override using the
## `.groups` argument.
# 繪製散點圖
ggplot(total_income, aes(x = Quarter, y = `Profit Before Tax`, color = Category)) +
geom_point(size = 3) +
geom_line(aes(group = Category), linetype = "dotted") +
labs(x = "Quarter", y = "Profit Before Tax (million KRW)", color = "Category") +
ggtitle("Profit Before Tax Comparison by Category in 2022 and 2023") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))