Skip to content

pasipasi123/gt_testi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Trying out the gt package

Pasi Haapakorva Sun Jan 20 20:34:13 2019

library(gt)
library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------------------------------ tidyverse 1.2.1 --

## v ggplot2 3.1.0.9000     v purrr   0.2.5     
## v tibble  2.0.1          v dplyr   0.8.0     
## v tidyr   0.8.2          v stringr 1.3.1     
## v readr   1.3.1          v forcats 0.3.0

## -- Conflicts --------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(here)
## here() starts at C:/Users/pasih_000/Documents/rprojektit/gt_testi
# Here I'm loading data acquired from the City of Oulu about the number of cyclist passing an ecocounter

hep <- fs::dir_ls(here("data")) %>%
   str_subset("ID_(89|96)") %>%
   map(~ read_csv2(., col_types = "ccdcccdddddd", locale = locale(encoding = "ISO-8859-1")) %>% as_tibble())
## Using ',' as decimal and '.' as grouping mark. Use read_delim() for more control.

## Using ',' as decimal and '.' as grouping mark. Use read_delim() for more control.
hep2 <- hep %>%
   bind_rows() %>%
   rename_all(tolower) %>%
   mutate(kuukausi = as.numeric(kuukausi)) %>%
   group_by(mittausid, vuosi, kuukausi) %>%
   summarise(pp = sum(pp_yht)) %>%
   ungroup() %>%
   complete(mittausid, vuosi, kuukausi, fill = list(pp = 0)) %>%
   filter(!(vuosi == 2018 & kuukausi > 9))

hep2 %>%
   ggplot(aes(lubridate::make_date(vuosi, kuukausi), pp, color = mittausid)) +
   geom_line()

# There seems to be some problems with the data. Some periods have less
# observations. Let's replace them with previous years' values.

hep2[c(hep2$kuukausi %in% 8:11 & hep2$mittausid == 96 & hep2$vuosi == 2012), 4] <-
   hep2[c(hep2$kuukausi %in% 8:11 & hep2$mittausid == 96 & hep2$vuosi == 2011), 4]

hep2[c(hep2$kuukausi %in% 4:6 & hep2$mittausid == 96 & hep2$vuosi == 2013), 4] <-
   hep2[c(hep2$kuukausi %in% 4:6 & hep2$mittausid == 96 & hep2$vuosi == 2012), 4]

hep2[c(hep2$kuukausi %in% 10:12 & hep2$mittausid == 96 & hep2$vuosi == 2014), 4] <-
   hep2[c(hep2$kuukausi %in% 10:12 & hep2$mittausid == 96 & hep2$vuosi == 2013), 4]

hep2 %>%
   ggplot(aes(lubridate::make_date(vuosi, kuukausi), pp, color = mittausid)) +
   geom_line()

# I want to compare later years to mean of the first three years.
# Year 2018 is incomplete, so I'll calculate another mean for it.

pp_all_mean <- hep2 %>%
   group_by(mittausid, vuosi) %>%
   summarise(pp_all = sum(pp)) %>%
   group_by(mittausid) %>%
   mutate(pp_all_mean = mean(pp_all[vuosi %in% 2011:2013])) %>%
   distinct(mittausid, pp_all_mean)

pp_9_mean <- hep2 %>%
   filter(kuukausi < 10) %>%
   group_by(mittausid, vuosi) %>%
   summarise(pp_all = sum(pp)) %>%
   group_by(mittausid) %>%
   mutate(pp_9_mean = mean(pp_all[vuosi %in% 2011:2013])) %>%
   distinct(mittausid, pp_9_mean)

pp_means <- pp_all_mean %>%
   left_join(pp_9_mean)
## Joining, by = "mittausid"
gt_data <- hep2 %>%
   group_by(mittausid, vuosi) %>%
   summarise(pp_all = sum(pp)) %>%
   left_join(pp_means) %>%
   mutate(osuus = case_when(vuosi == 2018 ~ pp_all / pp_9_mean - 1,
                            TRUE ~ pp_all / pp_all_mean - 1)) %>%
   ungroup() %>%
   mutate(mittausid = case_when(mittausid == "89" ~ "Hupisaaret",
                                TRUE ~ "Ouluhalli"))
## Joining, by = "mittausid"
# Building the `gt` table

gt_pp <- gt_data %>%
   gt(rowname_col = "vuosi", groupname_col = "mittausid") %>%
   cols_hide(vars(pp_all_mean, pp_9_mean)) %>%
   cols_label(pp_all = "Pyöräliikenne, lkm", osuus = "Vertailuluku") %>%
   fmt_percent(columns = vars(osuus), sep_mark = " ", dec_mark = ",", decimals = 1, incl_space = TRUE) %>%
   fmt_number(columns = vars(pp_all), sep_mark = " ", dec_mark = ",", decimals = 0)

# It looks like some of the formatting is lost in this markdown file, eg. alignment.

gt_pp
<style>html { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif; } #oywzymclsi .gt_table { display: table; border-collapse: collapse; margin-left: auto; margin-right: auto; color: #000000; font-size: 16px; background-color: #FFFFFF; /* table.background.color */ width: auto; /* table.width */ border-top-style: solid; /* table.border.top.style */ border-top-width: 2px; /* table.border.top.width */ border-top-color: #A8A8A8; /* table.border.top.color */ } #oywzymclsi .gt_heading { background-color: #FFFFFF; /* heading.background.color */ border-bottom-color: #FFFFFF; } #oywzymclsi .gt_title { color: #000000; font-size: 125%; /* heading.title.font.size */ padding-top: 4px; /* heading.top.padding */ padding-bottom: 1px; border-bottom-color: #FFFFFF; border-bottom-width: 0; } #oywzymclsi .gt_subtitle { color: #000000; font-size: 85%; /* heading.subtitle.font.size */ padding-top: 1px; padding-bottom: 4px; /* heading.bottom.padding */ border-top-color: #FFFFFF; border-top-width: 0; } #oywzymclsi .gt_bottom_border { border-bottom-style: solid; /* heading.border.bottom.style */ border-bottom-width: 2px; /* heading.border.bottom.width */ border-bottom-color: #A8A8A8; /* heading.border.bottom.color */ } #oywzymclsi .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #A8A8A8; padding-top: 4px; padding-bottom: 4px; } #oywzymclsi .gt_col_heading { color: #000000; background-color: #FFFFFF; /* column_labels.background.color */ font-size: 16px; /* column_labels.font.size */ font-weight: initial; /* column_labels.font.weight */ vertical-align: middle; padding: 10px; margin: 10px; } #oywzymclsi .gt_sep_right { border-right: 5px solid #FFFFFF; } #oywzymclsi .gt_group_heading { padding: 8px; color: #000000; background-color: #FFFFFF; /* stub_group.background.color */ font-size: 16px; /* stub_group.font.size */ font-weight: initial; /* stub_group.font.weight */ border-top-style: solid; /* stub_group.border.top.style */ border-top-width: 2px; /* stub_group.border.top.width */ border-top-color: #A8A8A8; /* stub_group.border.top.color */ border-bottom-style: solid; /* stub_group.border.bottom.style */ border-bottom-width: 2px; /* stub_group.border.bottom.width */ border-bottom-color: #A8A8A8; /* stub_group.border.bottom.color */ vertical-align: middle; } #oywzymclsi .gt_empty_group_heading { padding: 0.5px; color: #000000; background-color: #FFFFFF; /* stub_group.background.color */ font-size: 16px; /* stub_group.font.size */ font-weight: initial; /* stub_group.font.weight */ border-top-style: solid; /* stub_group.border.top.style */ border-top-width: 2px; /* stub_group.border.top.width */ border-top-color: #A8A8A8; /* stub_group.border.top.color */ border-bottom-style: solid; /* stub_group.border.bottom.style */ border-bottom-width: 2px; /* stub_group.border.bottom.width */ border-bottom-color: #A8A8A8; /* stub_group.border.bottom.color */ vertical-align: middle; } #oywzymclsi .gt_striped { background-color: #f2f2f2; } #oywzymclsi .gt_row { padding: 10px; /* row.padding */ margin: 10px; vertical-align: middle; } #oywzymclsi .gt_stub { border-right-style: solid; border-right-width: 2px; border-right-color: #A8A8A8; padding-left: 12px; } #oywzymclsi .gt_stub.gt_row { background-color: #FFFFFF; } #oywzymclsi .gt_summary_row { background-color: #FFFFFF; /* summary_row.background.color */ padding: 6px; /* summary_row.padding */ text-transform: inherit; /* summary_row.text_transform */ } #oywzymclsi .gt_first_summary_row { border-top-style: solid; border-top-width: 2px; border-top-color: #A8A8A8; } #oywzymclsi .gt_table_body { border-top-style: solid; /* field.border.top.style */ border-top-width: 2px; /* field.border.top.width */ border-top-color: #A8A8A8; /* field.border.top.color */ border-bottom-style: solid; /* field.border.bottom.style */ border-bottom-width: 2px; /* field.border.bottom.width */ border-bottom-color: #A8A8A8; /* field.border.bottom.color */ } #oywzymclsi .gt_footnote { font-size: 90%; /* footnote.font.size */ padding: 4px; /* footnote.padding */ } #oywzymclsi .gt_sourcenote { font-size: 90%; /* sourcenote.font.size */ padding: 4px; /* sourcenote.padding */ } #oywzymclsi .gt_center { text-align: center; } #oywzymclsi .gt_left { text-align: left; } #oywzymclsi .gt_right { text-align: right; font-variant-numeric: tabular-nums; } #oywzymclsi .gt_font_normal { font-weight: normal; } #oywzymclsi .gt_font_bold { font-weight: bold; } #oywzymclsi .gt_font_italic { font-style: italic; } #oywzymclsi .gt_super { font-size: 65%; } #oywzymclsi .gt_footnote_glyph { font-style: italic; font-size: 65%; } </style>
Pyöräliikenne, lkm Vertailuluku
Hupisaaret
2011 947 328 4,5 %
2012 863 748 -4,7 %
2013 907 509 0,1 %
2014 856 681 -5,5 %
2015 857 191 -5,4 %
2016 818 306 -9,7 %
2017 731 361 -19,3 %
2018 610 149 -16,5 %
Ouluhalli
2011 631 486 2,4 %
2012 605 763 -1,8 %
2013 612 616 -0,6 %
2014 631 236 2,4 %
2015 670 178 8,7 %
2016 646 460 4,8 %
2017 632 357 2,6 %
2018 507 098 7,0 %

About

Trying out the `gt` package in R

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published