knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(hgchmagic)

Introduction

hgchmagic has a set of functions to assists users when plotting data. Here you´ll see how to use it.

Bar plots

The core function to plot a bar plot with hgchmagic is hgch_bar()

# Load libraries
library(ggplot2)
library(dsdataprep)

# Load the data
data <- diamonds
data <- aggregation_data(data = data,
                         agg = "sum",
                         group_var = "cut",
                         to_agg = "price")

# Plot
hgch_bar(data, 
         var_cat = "cut", 
         var_num = "price")

scatter plots

# Load libraries
library(ggplot2)
library(dplyr)

# Load the data
data <- diamonds
data <- data |> select(carat, x, everything())

# plot
hgch_scatter(data, 
             var_num = c("x", "carat"))

line plots

# Load libraries
library(lubridate)
library(tidyr)
library(dplyr)

# Load the data
data <- lakers
data$date <- ymd(data$date)

data <- data |>
  drop_na(x) |>
  group_by(date) |>
  summarise(x = sum(x)) |> 
  arrange(date)

# Plot
hgch_line(data, 
          var_dat = "date", 
          var_num = "x", 
          hor_title = "fecha", 
          ver_title = "valor")

pie plots

# Load libraries
library(ggplot2)
library(dsdataprep)

# Load the datas
data <- diamonds
data <- aggregation_data(data = data,
                         agg = "sum",
                         group_var = "cut",
                         to_agg = "price")

# Plot
hgch_pie(data = data, 
         var_cat = "cut", 
         var_num = "price")

Sankey plot

# Load libraries
library(ggplot2)
library(dplyr)

# Load the datas
data <- diamonds

data <- data |> 
  group_by(cut, clarity) |> 
  summarise(total = sum(z, na.rm = T))

hgch_sankey(data, 
            var_cat = c("cut", "clarity"), 
            var_num = "total")

Treemap plot

# Load libraries
library(ggplot2)
library(dplyr)

# Load the data
data <- diamonds |> 
  select(cut, everything())

# Plot
hgch_treemap_Cat(data)

Customization

Plots with hgchmagic can be easily improved by adding some labels of information like titles, subtitles, caption, etc.:

# Load libraries
library(ggplot2)
library(dplyr)

### hgch_bar_Cat()
data <- diamonds |> 
  select(cut, everything())

## List with different custom settings for the plot
ops <- list(title = "This is a title",
            subtitle = "This is a subtitle",
            caption = "A caption? Yes, this is a caption",
            hor_title = "Categories",
            ver_title = "Numbers",
            bar_orientation = "hor")

# Plot
hgch_bar(data, 
         var_cat = "cut", 
         var_num = "price", 
         opts =  ops)


hgchmagic can help you plotting identifying different types of variables in the data frame. For example, when having a Categorical variable the hgch_bar_Cat() function could be more helpful:

### hgch_bar_Cat()
hgch_bar_Cat(data, 
             opts = ops, 
             palette_colors = "#ffa92a")

More functions that plots according to variable types:

data <- diamonds |> 
  select(cut, color, price, everything())

hgch_bar_CatCat(data, 
                opts = ops)
hgch_bar_CatCatNum(data, 
                   opts = ops)


Plots with custom theme setted on opts argument:

# Data
data <- diamonds |> 
  select(cut, everything())

# opts from theme (canvas)
test_theme <- list(
  theme = list(
    background_color = "#2f2f2f",
    plot_margin_bottom = 30,
    plot_margin_left = 30,
    plot_margin_right = 30,
    plot_margin_top = 30,
    plot_background_color = "#f2f2f2",
    plot_border_color = "#ff2c2f",
    plot_border_size = 3,
    text_family = "ubuntu",
    text_size = 15
  )
)

# Plot
hgch_bar_Cat(data, 
             opts = test_theme)




randommonkey/hgchmagic documentation built on Aug. 23, 2023, 5:56 a.m.