Code
library(ggplot2)
library(dplyr)
library(patchwork)定期券: 1ヶ月 vs 3ヶ月 vs 6ヶ月
ggplot2, dplyr and patchwork
library(ggplot2)
library(dplyr)
library(patchwork)市民広場前〜三宮 250円, 三宮〜姫路 990円(1,240円)
# set one way fee for each line
pl_single <- 250 # port liner ShiminHiroba-Sannomiya
jr_single <- 990 # JR West Sannomiya-Himeji通学定期券
| 種別 | 1ヶ月 | 3ヶ月 | 6ヶ月 |
|---|---|---|---|
| Port Liner | 4,830 | 13,770 | 24,640 |
| JR West | 13,900 | 39,900 | 75,550 |
| Total | 18,730 | 53,670 | 100,190 |
# Student fares
# port liner
pl_monthly_pass_student <- 4830
pl_three_month_pass_student <- 13770
pl_six_month_pass_student <- 24640
# jr
jr_monthly_pass_student <- 13980
jr_three_month_pass_student <- 39900
jr_six_month_pass_student <- 75550
# combination
# combination of port liner and jr single trip fees
combined_single <- pl_single + jr_single
# combination of monthly for port liner and jr of student
combined_monthly_pass_student <- pl_monthly_pass_student +
jr_monthly_pass_student
# combination of three month for port liner and jr of student
combined_three_month_pass_student <- pl_three_month_pass_student +
jr_three_month_pass_student
# combination of six month for port liner and jr of student
combined_six_month_pass_student <- pl_six_month_pass_student +
jr_six_month_pass_student通勤定期券
| 種別 | 1ヶ月 | 3ヶ月 | 6ヶ月 |
|---|---|---|---|
| Port Liner | 9,650 | 27,510 | 49,220 |
| JR West | 28,010 | 79,850 | 142,560 |
| Total | 37,660 | 107,360 | 191,780 |
# Adult fares
# port liner
pl_monthly_pass_adult <- 9650
pl_three_month_pass_adult <- 27510
pl_six_month_pass_adult <- 49220
# jr
jr_monthly_pass_adult <- 28100
jr_three_month_pass_adult <- 79850
jr_six_month_pass_adult <- 142560
# combination
# combination of port liner and jr single trip fees
combined_single <- pl_single + jr_single
# Combined monthly pass fare for adults
combined_monthly_pass_adult <- pl_monthly_pass_adult +
jr_monthly_pass_adult
# Combined three-month pass fare for adults
combined_three_month_pass_adult <- pl_three_month_pass_adult +
jr_three_month_pass_adult
# Combined six-month pass fare for adults
combined_six_month_pass_adult <- pl_six_month_pass_adult +
jr_six_month_pass_adult6ヶ月定期券の利用可能日数は180日。プロットは78日で出力した。
# student and adult fares
monthly_pass <- function(title,
single_trip,
monthly_pass_student,
monthly_pass_adult,
three_month_pass_student,
three_month_pass_adult,
six_month_pass_student,
six_month_pass_adult){
# Create a sequence of numbers from 1 to xxx
# Number of round trips and calculate the cost
df <- data.frame(days = 1:78)
df <- df %>%
mutate(trip_cost = single_trip * days * 2) # times 2 is for round trip
# Student's 'label_data' to store the x and y coordinates and the label
label_data <- data.frame(
x = c(30, 43, 60),
y = c(monthly_pass_student,
three_month_pass_student,
six_month_pass_student),
label = sprintf("%s Months: JPY %s.-", c("Monthly", "3(90days)", "6(180days)"),
format(c(monthly_pass_student,
three_month_pass_student,
six_month_pass_student),
big.mark = ",", scientific = FALSE))
)
# Adult's 'label_data' to store the x and y coordinates and the label
label_data_adult <- data.frame(
x = c(36, 26, 60),
y = c(monthly_pass_adult,
three_month_pass_adult,
six_month_pass_adult),
label = sprintf("%s Months: JPY %s.-", c("Monthly", "3(90days)", "6(180days)"),
format(c(monthly_pass_adult,
three_month_pass_adult,
six_month_pass_adult),
big.mark = ",", scientific = FALSE))
)
# Plot line, circles and text
plot <- ggplot(df, aes(x = days, y = trip_cost)) +
geom_line(color = "blue", linewidth = 2) + # plot blue line
geom_point(shape = 21, size = 5,
color = "blue", fill = "white") + # plot white circles
geom_text(aes(label = days), vjust = 0.5,
color = "black") + # black text in the circles
# plot horizontal lines for student's the monthly, three-month and six-month passes
geom_hline(yintercept = c(monthly_pass_student,
three_month_pass_student,
six_month_pass_student),
color = "red", linewidth = 1) +
# plot horizontal lines for adult's the monthly, three-month and six-month passes
geom_hline(yintercept = c(monthly_pass_adult,
three_month_pass_adult,
six_month_pass_adult),
color = "black", linewidth = 1) +
# plot the labels for student's the monthly, three-month and six-month passes
geom_text(data = label_data,
aes(x = x, y = y, label = label),
vjust = -0.5, color = "red", size = 3) +
# plot the labels for adult's the monthly, three-month and six-month passes
geom_text(data = label_data_adult,
aes(x = x, y = y, label = label),
vjust = -0.5, color = "black", size = 3) +
# set the title and axis labels
ggtitle(title) +
xlab("Number of Days") +
ylab("Cost (JPY)") +
# set the theme, font family, size and weight
theme_set(theme_bw(base_size = 12, base_family = "Times New Roman") +
theme(plot.title = element_text(face = "bold", size = 14),
axis.title = element_text(face = "bold")))
return(plot)
}# plot the cost of port liner's trips only for Adult
p1 <- monthly_pass("Cost of Port Liner",
pl_single,
pl_monthly_pass_student,
pl_monthly_pass_adult,
pl_three_month_pass_student,
pl_three_month_pass_adult,
pl_six_month_pass_student,
pl_six_month_pass_adult)
# plot the cost of JR liner's trips
p2 <- monthly_pass("Cost of JR West",
jr_single,
jr_monthly_pass_student,
jr_monthly_pass_adult,
jr_three_month_pass_student,
jr_three_month_pass_adult,
jr_six_month_pass_student,
jr_six_month_pass_adult)
p3 <- monthly_pass("Cost of Combined",
combined_single,
combined_monthly_pass_student,
combined_monthly_pass_adult,
combined_three_month_pass_student,
combined_three_month_pass_adult,
combined_six_month_pass_student,
combined_six_month_pass_adult)# Combine the three plots
p3# Combine the three plots 横並びはncol = 3, 縦はncol =1)
p1 + p2 # + plot_layout(ncol = 2)plot_graphs <- function (prices) {
for (p in prices) {
print(monthly_pass(p$title,
p$single_trip,
p$monthly_pass_student,
p$monthly_pass_adult,
p$three_month_pass_student,
p$three_month_pass_adult,
p$six_month_pass_student,
p$six_month_pass_adult))
}
}
combined <- list(title="Cost of Combined trips",
single_trip = combined_single,
monthly_pass_student = combined_monthly_pass_student,
monthly_pass_adult = combined_monthly_pass_adult,
three_month_pass_student = combined_three_month_pass_student,
three_month_pass_adult = combined_three_month_pass_adult,
six_month_pass_student = combined_six_month_pass_student,
six_month_pass_adult = combined_six_month_pass_adult)
port_liner <- list(title="Cost of Port Liner",
single_trip = pl_single,
monthly_pass_student = pl_monthly_pass_student,
monthly_pass_adult = pl_monthly_pass_adult,
three_month_pass_student = pl_three_month_pass_student,
three_month_pass_adult = pl_three_month_pass_adult,
six_month_pass_student = pl_six_month_pass_student,
six_month_pass_adult = pl_six_month_pass_adult)
jr <- list(title="Cost of JR West",
single_trip = jr_single,
monthly_pass_student = jr_monthly_pass_student,
monthly_pass_adult = jr_monthly_pass_adult,
three_month_pass_student = jr_three_month_pass_student,
three_month_pass_adult = jr_three_month_pass_adult,
six_month_pass_student = jr_six_month_pass_student,
six_month_pass_adult = jr_six_month_pass_adult)
prices <- list(port_liner, jr, combined)
plot_graphs(prices)Figure 1 Combined Adult
p3Figure 2 Port Liner Adult
p1Figure 3 JR West Adult
p2