rm(list  =  ls())

if(!require("pacman")) install.packages("pacman")
##  要求されたパッケージ pacman をロード中です
pacman::p_load("tidyverse",  
               "skimr",     #  記述統計のパッケージ
               "gt")      #  表(table)をデータフレームから作るパッケージ

# 表示を科学表示から変更
 options(scipen = 999)
dat_temp <- read_csv("QFR2023TermPaper.csv")
## New names:
## Rows: 350 Columns: 5
## ── Column specification
## ────────────────────────────────────────────────────────
## Delimiter: "," chr (1): Fish_Market dbl (4): ...1, Year, Month, Total
## ℹ 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.
## • `` -> `...1`
skimr::skim(dat_temp)
Data summary
Name dat_temp
Number of rows 350
Number of columns 5
_______________________
Column type frequency:
character 1
numeric 4
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
Fish_Market 0 1 8 8 0 2 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
…1 0 1 175.50 101.18 1 88.25 175.5 262.75 350 ▇▇▇▇▇
Year 0 1 2005.13 6.92 1994 1999.00 2005.0 2011.00 2019 ▇▇▇▆▃
Month 0 1 8.33 2.91 1 7.00 9.0 11.00 12 ▂▁▅▆▇
Total 0 1 2317.37 3038.65 0 1.00 640.0 4176.50 13095 ▇▂▂▁▁
summary_1 <- dat_temp |> group_by(Year, Month) |> summarize(total_landing = sum(Total))
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
fig_1 <- ggplot(summary_1, aes(x = Month, y = total_landing, fill = Year)) + geom_bar(stat = "identity", position = "dodge")

fig_1

summary_1 <- dat_temp |> group_by(Year, Month) |> 
  summarize(total_landing = sum(Total)) |>
  mutate(Month = as.factor(Month), Year = as.factor(Year)) |> ungroup()
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
fig_2 <-  ggplot(summary_1, aes(x = Month, y = total_landing, fill = Year)) + 
            geom_bar(stat = "identity", position = "dodge")

fig_2

table(dat_temp$Fish_Market)
## 
## Market_A Market_B 
##      183      167
dat_temp <- dat_temp|> filter(Fish_Market %in% c("Market_A")) 
summary_1 <- dat_temp |> group_by(Year, Month) |> 
  summarize(total_landing = sum(Total)) |>
  mutate(Month = as.factor(Month), Year = as.factor(Year)) |> ungroup()
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
fig_3 <-  ggplot(summary_1, aes(x = Month, y = total_landing, fill = Year)) + 
            geom_bar(stat = "identity", position = "dodge")

fig_3

dat_temp2 <- read_csv("QFR2023TermPaper.csv")
## New names:
## Rows: 350 Columns: 5
## ── Column specification
## ────────────────────────────────────────────────────────
## Delimiter: "," chr (1): Fish_Market dbl (4): ...1, Year, Month, Total
## ℹ 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.
## • `` -> `...1`
dat_temp2 <- dat_temp2|> filter(Fish_Market %in% c("Market_B")) 
summary_2 <- dat_temp2 |> group_by(Year, Month) |> 
  summarize(total_landing = sum(Total)) |>
  mutate(Month = as.factor(Month), Year = as.factor(Year)) |> ungroup()
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
fig_4 <-  ggplot(summary_2, aes(x = Month, y = total_landing, fill = Year)) + 
            geom_bar(stat = "identity", position = "dodge")

fig_4

dat_temp3 <- read_csv("kadai.csv")
## New names:
## Rows: 140 Columns: 6
## ── Column specification
## ────────────────────────────────────────────────────────
## Delimiter: "," chr (1): Date num (1): Landing_Amount lgl (4): ...3, ...4, ...5,
## ...6
## ℹ 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.
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
skimr::skim(dat_temp3)
Data summary
Name dat_temp3
Number of rows 140
Number of columns 6
_______________________
Column type frequency:
character 1
logical 4
numeric 1
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
Date 1 0.99 8 9 0 139 0

Variable type: logical

skim_variable n_missing complete_rate mean count
…3 140 0 NaN :
…4 140 0 NaN :
…5 140 0 NaN :
…6 140 0 NaN :

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Landing_Amount 1 0.99 6772625 6693217 4094 862112.5 4643085 12171277 22943560 ▇▂▂▂▁
dat_temp3 <- dat_temp3 |> mutate(Date = lubridate::ymd(Date)) 
dat_temp3 <- dat_temp3 |> 
  mutate(YEAR = lubridate::year(Date), 
         MONTH = lubridate::month(Date), 
         DAY = lubridate::day(Date))
skimr::skim(dat_temp3)
Data summary
Name dat_temp3
Number of rows 140
Number of columns 9
_______________________
Column type frequency:
Date 1
logical 4
numeric 4
________________________
Group variables None

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
Date 1 0.99 1994-09-01 2023-09-01 2009-11-01 139

Variable type: logical

skim_variable n_missing complete_rate mean count
…3 140 0 NaN :
…4 140 0 NaN :
…5 140 0 NaN :
…6 140 0 NaN :

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Landing_Amount 1 0.99 6772624.97 6693217.36 4094 862112.5 4643085 12171277 22943560 ▇▂▂▂▁
YEAR 1 0.99 2008.81 8.42 1994 2001.5 2009 2016 2023 ▇▇▇▇▇
MONTH 1 0.99 9.81 1.41 6 9.0 10 11 12 ▁▅▅▅▇
DAY 1 0.99 1.00 0.00 1 1.0 1 1 1 ▁▁▇▁▁
summary_3 <- dat_temp3 |> group_by(YEAR, MONTH) |> summarize(total_landing = sum(Landing_Amount))
## `summarise()` has grouped output by 'YEAR'. You can override using the
## `.groups` argument.
fig_5 <- ggplot(summary_3, aes(x = MONTH, y = total_landing, fill = YEAR)) + geom_bar(stat = "identity", position = "dodge")

fig_5
## Warning: Removed 1 rows containing missing values (`geom_bar()`).

summary_3 <- dat_temp3 |> group_by(YEAR, MONTH) |> 
  summarize(total_landing = sum(Landing_Amount)) |>
  mutate(MONTH = as.factor(MONTH), YEAR = as.factor(YEAR)) |> ungroup()
## `summarise()` has grouped output by 'YEAR'. You can override using the
## `.groups` argument.
fig_6 <-  ggplot(summary_3, aes(x = MONTH, y = total_landing, fill = YEAR)) + 
            geom_bar(stat = "identity", position = "dodge")

fig_6
## Warning: Removed 1 rows containing missing values (`geom_bar()`).