3  Membuat Tabel

3.1 Happiness WVS 7

library(dplyr) 

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
library(gt)
library(scales)

wvs = read.csv("happiness_asean.csv")

# gt kebahagiaan
happy <- select(wvs,Negara,Kebahagiaan) %>%
  arrange(desc(Kebahagiaan))

happy_tbl <- gt(happy) %>%
  tab_header(
    title = md("**Tingkat Kebahagiaan Negara ASEAN Menurut WVS 7**")) %>%
  tab_source_note(
    source_note = "Source: https://www.worldvaluessurvey.org/wvs.jsp"
  ) 
happy
     Negara Kebahagiaan
1  Malaysia       1.972
2 Singapura       1.860
3  Thailand       1.845
4   Myanmar       1.825
5 Indonesia       1.630
6  Filipina       1.584
7   Vietnam       1.552
happy <- select(wvs,Negara,Kebahagiaan) %>%
  arrange(desc(Kebahagiaan))

happy_tbl <- gt(happy) %>%
  tab_header(
    title = md("**Tingkat Kebahagiaan Negara ASEAN Menurut WVS 7**")) %>%
  tab_source_note(
    source_note = "Source: https://www.worldvaluessurvey.org/wvs.jsp"
  ) 

min_hepi <- min(happy$Kebahagiaan)
max_hepi <- max(happy$Kebahagiaan)
hepi_palette <- col_numeric(c("#55C667FF","#1F968BFF"), domain = c(max_hepi, min_hepi), alpha = 0.75)

(happy_table <- happy_tbl %>% 
    data_color(columns = c(Kebahagiaan),
               colors = hepi_palette))
Warning: Since gt v0.9.0, the `colors` argument has been deprecated.
• Please use the `fn` argument instead.
This warning is displayed once every 8 hours.
Tingkat Kebahagiaan Negara ASEAN Menurut WVS 7
Negara Kebahagiaan
Malaysia 1.972
Singapura 1.860
Thailand 1.845
Myanmar 1.825
Indonesia 1.630
Filipina 1.584
Vietnam 1.552
Source: https://www.worldvaluessurvey.org/wvs.jsp
(happy_tbl <- happy_tbl%>% 
    #All column headers are capitalised
    opt_all_caps() %>% 
    #Use the Chivo font
    #Note the great 'google_font' function in 'gt' that removes the need to pre-load fonts
    opt_table_font(
      font = list(
        google_font("Chivo"),
        default_fonts()
      )
    ) %>%
    #Change the width of columns
    cols_width(c(Kebahagiaan) ~ px(150),
               c(Negara) ~ px(400)) %>% 
    tab_options(
      #Remove border between column headers and title
      column_labels.border.top.width = px(3),
      column_labels.border.top.color = "transparent",
      #Remove border around table
      table.border.top.color = "transparent",
      table.border.bottom.color = "transparent",
      #Reduce the height of rows
      data_row.padding = px(3),
      #Adjust font sizes and alignment
      source_notes.font.size = 12,
      heading.align = "left"
    ))
Tingkat Kebahagiaan Negara ASEAN Menurut WVS 7
Negara Kebahagiaan
Malaysia 1.972
Singapura 1.860
Thailand 1.845
Myanmar 1.825
Indonesia 1.630
Filipina 1.584
Vietnam 1.552
Source: https://www.worldvaluessurvey.org/wvs.jsp

3.2 add image

#To convert country codes
library(countrycode)

flag_db <- read.csv("Country_Flags.csv") %>% 
  #Convert country names into 3-letter country codes
  mutate(Code = countrycode(sourcevar =Country, origin = "country.name", destination = "iso3c", warn = FALSE)) %>% 
  select(Code, flag_URL = ImageURL)

flag_data <- wvs %>% 
  left_join(flag_db, by = "Code") %>% 
  select(flag_URL, Negara, everything())

#We'll need to refit our table using this new data
#Code below with comments removed.
wb_table <- head(flag_data) %>%
  gt() %>% 
  cols_hide(columns = c(Code)) %>% 
  cols_label(Negara = "Country",
             Kebahagiaan = "Kebahagiaan") %>% 
  tab_header(title = md("Tingkat Kebahagiaan Negara ASEAN")) %>%
  tab_source_note(source_note = "Sumber Data: World Values Survey 7 ") %>% 
  fmt_number(columns = c(Kebahagiaan),
             scale_by = 1) %>%
  cols_label(Kebahagiaan = "Kebahagiaan") %>% 
  tab_style(
    locations = cells_column_labels(columns = everything()),
    style     = list(
      cell_borders(sides = "bottom", weight = px(3)),
      cell_text(weight = "bold")
    )
  ) %>% 
  tab_style(
    locations = cells_title(groups = "title"),
    style     = list(
      cell_text(weight = "bold", size = 25)
    )
  ) %>% 
  data_color(columns = c(Kebahagiaan),
             colors = hepi_palette) %>% 
  opt_all_caps() %>% 
  opt_table_font(
    font = list(
      google_font("Chivo"),
      default_fonts()
    )
  ) %>%
  cols_width(c(Kebahagiaan) ~ px(150),
             c(Negara) ~ px(300)) %>% 
  tab_options(
    column_labels.border.top.width = px(3),
    column_labels.border.top.color = "transparent",
    table.border.top.color = "transparent",
    table.border.bottom.color = "transparent",
    data_row.padding = px(3),
    source_notes.font.size = 12,
    heading.align = "center")

wb_table %>% 
  gt::text_transform(
    #Apply a function to a column
    locations = cells_body(c(flag_URL)),
    fn = function(x) {
      #Return an image of set dimensions
      web_image(
        url = x,
        height = 12
      )
    }
  ) %>% 
  #Hide column header flag_URL and reduce width
  cols_width(c(flag_URL) ~ px(30)) %>% 
  cols_label(flag_URL = "")
Tingkat Kebahagiaan Negara ASEAN
Country Kebahagiaan
Malaysia 1.97
Singapura 1.86
Thailand 1.84
Myanmar 1.82
Indonesia 1.63
Filipina 1.58
Sumber Data: World Values Survey 7