Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/evamaerey/ggverbatim
tabular data to tabular viz
https://github.com/evamaerey/ggverbatim
Last synced: 11 days ago
JSON representation
tabular data to tabular viz
- Host: GitHub
- URL: https://github.com/evamaerey/ggverbatim
- Owner: EvaMaeRey
- Created: 2022-07-20T17:19:53.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-04-29T16:04:30.000Z (6 months ago)
- Last Synced: 2024-04-29T17:28:16.487Z (6 months ago)
- Language: R
- Size: 957 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# ggverbatim
Some tables have body's with values that are like-in-kind (LIK tables). These tables differ from data-frames, as most data-frames columns' contain values of different types.
Tables that have bodies with like-in-kind values can arise from cross tabulation, correlation analyses, and storage of time series data, for example.
```{r, examples}
Titanic[ , , Age = "Child", Survived = "No"]
```To use such data with the popular data visualization tool ggplot2, data is usually pivoted to its long form, where most of the column names (e.g. years in time series wide storage) are stored in a single column.
```{r}
```
ggverbatim aims to faithfully reproduce input LIK in ggplot2 without the pivoting step. Thus, products like heat maps and correlations tables can be prepped as tables and directly ported to a ggplot2 build via the ggverbatim() function without pivoting to longer. Users also won't need without specifying factor ordering. Thus, we provide an interface for a close visual match between the table input and the visual output.
## Installation
Install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("EvaMaeRey/ggverbatim")
```## Motivation
ggplot requires 'tidy' data, usually 'long'. You may build a table in ggplot2, the visual arrangement isn't tidy, but is nice for humans.
Sometimes, you will have data in an untidy format (your raw data or data that you've already worked with). And you might just want to reproduce it ggplot2 'verbatim'.
Currently, this would be accomplished via a pivot to long ('unpivot') and then wide-pivoted through the visual specification. That is what ggverbatim actually does under the hood. But if feels like a A1 -> A2 process, not an A1 -> B -> A2 process.
```{r example}
# library(ggverbatim)
library(tidyverse, verbose = F)
library(magrittr)Titanic %>%
data.frame() %>%
tibble() %>%
uncount(Freq) %>%
count(Survived, Sex) %>%
pivot_wider(names_from = Sex, values_from = n) ->
vis_arrangementvis_arrangement
``````{r cars}
vis_arrangement %>%
pivot_longer(cols = -1) %>%
ggplot() +
aes(x = name) +
labs(x = "Sex") +
aes(y = Survived) +
aes(label = value) +
aes(fill = value) +
geom_tile() +
geom_text() +
scale_x_discrete(position = "top") +
scale_y_discrete(limits=rev)
```# Therefore
```{r}
knitrExtra:::chunk_to_r("ggverbatim")
``````{r ggverbatim}
#' Title
#'
#' @param data
#' @param row_var_name
#' @param cols_var_name
#'
#' @return
#' @export
#'
#' @examples
#' Titanic %>%
#' data.frame() %>%
#' tibble() %>%
#' uncount(Freq) %>%
#' count(Survived, Sex) %>%
#' pivot_wider(names_from = Sex, values_from = n) ->
#' vis_arrangement
#'
#' vis_arrangement %>%
#' ggverbatim()
ggverbatim <- function(data, cat_cols = 1, row_var_name = NULL, cols_var_name = "x", value_var_name = NULL){message("Variables that represented visually are ; e.g. aesthetic mappying are 'x', and " |> paste(row_var_name))
row_var_name <- names(data)[1]
names(data)[1] <- "row_var"col_headers <- names(data)
col_headers <- col_headers[2:length(col_headers)]data %>%
mutate(row_var = fct_inorder(row_var)) %>%
pivot_longer(cols = -cat_cols) %>%
mutate(name = factor(name, levels = col_headers)) %>%
rename(x = name) ->
pivotedpivoted %>%
ggplot() +
aes(x = x) +
labs(x = cols_var_name) +
aes(y = row_var) +
labs(y = row_var_name) +
aes(label = value) +
aes(fill = value) +
scale_x_discrete(position = "top") +
scale_y_discrete(limits=rev)}
``````{r}
vis_arrangement %>%
ggverbatim() +
geom_tile(alpha = .8) +
aes(fill = value) +
aes(labels = value) +
geom_text(color = "oldlace") +
labs(x = "Sex") +
theme_minimal()
```# Another example; corrr
The corrr project is designed to make correlation fit the 'tidy' paradigmn. the output of correlate is treated like a data frames in the ggplot2 setting due to the dispatch methods used.
```{r}
library(corrr)
corrr_example <- correlate(mtcars)corrr_example
corrr_example %>%
ggverbatim() +
geom_tile() +
scale_fill_viridis_c()corrr_example %>%
ggverbatim() +
geom_point(aes(size = value))corrr_example %>%
shave(upper = FALSE) %>%
ggverbatim() +
geom_tile() +
geom_text(aes(label = round(value, 2), color = value >.1)) +
scale_fill_viridis_c()last_plot() +
geom_tile(data = . %>% filter(value > .7),
color = "red",
linewidth = 1.5,
fill = alpha("red", .2))```
# time series example
```{r, eval = F}
library(dplyr)
dat <- read_csv("data-raw/API_AG.LND.ARBL.ZS_DS2_en_csv_v2_5734536/API_AG.LND.ARBL.ZS_DS2_en_csv_v2_5734536.csv", skip = 3)dat %>%
arrange(-`1961`) %>%
select(`Country Name`, `1961`:`1970`) %>%
slice(1:10) %>%
ggverbatim() +
geom_tile(color = "white") +
geom_text(color = "white")```