My goal for this week is to learn how to use the GT package so that I’m able to use it for my verification report.
library(tidyverse)
library(dplyr)
library(gt)
## Warning: package 'gt' was built under R version 4.0.5
library(rmarkdown)
Data <- read.csv("Study 1 data.csv", header = TRUE)
# We turn Data into a gt table:
gt_Data <- gt(data = Data)
# Display the table
gt_Data
We created a simple table with columns and cells, but we can add more to it than that.
gt_Data <- gt_Data %>%
tab_header(
title = "**Wow a pretty title**",
subtitle = "*With it's baby subtitle*"
)
# Display table
gt_Data
I saw Wonjae’s learning log and saw that he was having trouble trying to tabulate the correlational matrix for our paper, so I thought I’d use his problem as an opportunity to learn gt().
Here is Wonjae’s code:
library(janitor)
## Warning: package 'janitor' was built under R version 4.0.5
library(plyr)
S1 <- read_csv("Study 1 Data.csv") %>%
clean_names() %>%
select(lethaverage_t1:extraversion) # We need to select only the variables we want to perform a pairwise correlation on
S1corup <- round(cor(S1), 2)
S1df <- as.data.frame(t(S1corup)) #Added to convert from matrix to df
I added the last line to convert the matrix into a dataframe. I did this because gt didn’t let me plot matrices.
gt_DataS1 <- S1df %>% # tabulate the data frame version
gt(rownames_to_stub = TRUE, ) %>% # the rownames_to_stub argument tells R to include the row names from the input table.
tab_options(table_body.hlines.color = "white", table_body.vlines.color = "white") # I managed to remove the cell lines with this code
gt_DataS1 # create table
| lethaverage_t1 | lethaverage_t2 | leth_diff | scaverage_t1 | scaverage_t2 | s_cdiff | extraversion | |
|---|---|---|---|---|---|---|---|
| lethaverage_t1 | 1.00 | 0.41 | -0.48 | -0.48 | -0.36 | 0.16 | -0.28 |
| lethaverage_t2 | 0.41 | 1.00 | 0.60 | -0.27 | -0.41 | -0.16 | -0.14 |
| leth_diff | -0.48 | 0.60 | 1.00 | 0.16 | -0.08 | -0.30 | 0.11 |
| scaverage_t1 | -0.48 | -0.27 | 0.16 | 1.00 | 0.66 | -0.45 | 0.56 |
| scaverage_t2 | -0.36 | -0.41 | -0.08 | 0.66 | 1.00 | 0.38 | 0.50 |
| s_cdiff | 0.16 | -0.16 | -0.30 | -0.45 | 0.38 | 1.00 | -0.09 |
| extraversion | -0.28 | -0.14 | 0.11 | 0.56 | 0.50 | -0.09 | 1.00 |
While I wasn’t able to completely replicate the study 1 table aesthetically, I definitely learned a lot about the gt package in the process. In week 9, I’ll be able to apply what I’ve learned to the tables in my study and keep working towards finishing my verification report.