SimplePie 1.5 is now available!

David Hodge

Recent content on David Hodge

{ggblanket}: common x and y scale adjustments 31 May 2022, 12:00 am

I was messing around with some scale adjustments with my {ggblanket} package, and thought I’d share this as a post.

It’s by no means a complete demonstration of the functionality available.

But it demonstrates a bit of how x and y scales can be adjusted.. as well as some other stuff randomly thrown in.

library(palmerpenguins)
library(tidyverse)
library(lubridate)
library(ggblanket)

Note x scale default limits of min/max breaks and zero expanding

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy)

Make the theme horizontal lines only

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy, 
         theme = gg_theme(y_grid = TRUE))

Adjust limits of an x date variable to the min and max of the data

economics %>%
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy, 
         x_limits = c(NA_Date_, NA_Date_))

Add expand of 0.05

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy, 
         x_limits = c(NA_Date_, NA_Date_),
         x_expand = c(0.05, 0.05))

Make more breaks using the pretty algorithm with the x_breaks_n argument.

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy, 
         x_breaks_n = 10)

Make breaks using the scales::fullseq algorithm using the x_breaks_width argument.

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy, 
         x_breaks_width = "3 years")

Specify your own specific breaks. In this case, we are using the seq function and working backwards to ensure our last break is the maximum of the data. Note that if your breaks do not cover all of the data, you will need to specify limits of c(NA, NA) or c(NA_Data_, NA_Data_) to include all of the data.

x_vctr <- economics %>% slice_head(n = 100) %>% pull(date)
x_breaks <- seq(from = max(x_vctr), to = min(x_vctr), by = "-19 months")

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy, 
         x_breaks = x_breaks,
         x_limits = c(NA_Date_, NA_Date_))

Use ggplot2’s default breaks, limits, labels and expand

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy) +
  scale_x_date()

Convert to an area graph

economics %>% 
  slice_head(n = 100) %>% 
  gg_area(x = date, 
          y = unemploy)

Convert to a line graph

economics %>% 
  slice_head(n = 100) %>% 
  gg_line(x = date, 
          y = unemploy,
          size = 1)

Add zero in the y scale

economics %>% 
  slice_head(n = 100) %>% 
  gg_line(x = date, 
          y = unemploy, 
          size = 1,
          y_zero = TRUE)

Convert to a step graph. And colour by the y variable. Note no legend is provided, as it is the values can be read off the y scale.

economics %>% 
  slice_head(n = 100) %>% 
  gg_step(x = date, 
          y = unemploy, 
          col = unemploy)

Convert to a ribbon to show uncertainty

economics %>% 
  slice_head(n = 100) %>% 
  gg_ribbon(date, 
            y = unemploy, 
            ymin = unemploy - 500, 
            ymax = unemploy + 500, 
            y_zero = TRUE) +
  geom_line()

Remove the outer lines

economics %>% 
  slice_head(n = 100) %>% 
  gg_ribbon(x = date, 
            y = unemploy, 
            ymin = unemploy - 500, 
            ymax = unemploy + 500, 
            y_zero = TRUE,
            pal = scales::alpha(pal_viridis_mix(1), 0)) +
  geom_line(col = scales::alpha(pal_viridis_mix(1), 1))

Change the y title

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy, 
         y_title = "Unemployment")

Remove the x title

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, 
         y = unemploy, 
         x_title = "")

Alternative way to remove the x title.

economics %>% 
  slice_head(n = 100) %>% 
  gg_col(x = date, y = unemploy) +
  ggplot2::labs(x = NULL)

For further information, see the ggblanket website.

{ggblanket}: simplify beautiful ggplot2 visualisation 14 May 2022, 12:00 am

A few days ago, I released {ggblanket} onto CRAN.

This package took over my brain for 1.5 months, and I worked obsessively on it. I hope people find it useful.

The objective of {ggblanket} is to make beautiful {ggplot2} visualisation simpler.

With this objective in mind, the {ggblanket} package:

  • uses quick functions that wrap around a single geom
  • merges col and fill aesthetics into a single col aesthetic
  • provides colour customisation via a pal argument
  • treats faceting as an aesthetic
  • pushes x and y limits to the max of the x and y breaks by default
  • arranges horizontal geom y and col labels etc to be in correct order
  • converts titles and labels to sentence case and comma format by default
  • provides arguments for scale adjustment and legend placement
  • allows users to access all other non-aesthetic geom functionality
  • changes default colours, alphas, widths and themes
library(dplyr)
library(ggplot2)
library(ggblanket)

penguins2 <- palmerpenguins::penguins %>% 
  tidyr::drop_na() %>% 
  mutate(body_mass_kg = body_mass_g / 1000) 
penguins2 %>% 
  ggplot() +
  geom_histogram(aes(x = body_mass_kg)) 

penguins2 %>% 
  gg_histogram(x = body_mass_kg) 

penguins2 %>%
  group_by(species, sex, island) %>%
  summarise(body_mass_kg = mean(body_mass_kg)) %>%
  ggplot() +
  geom_col(
    aes(x = body_mass_kg, y = species, fill = sex), 
    position = "dodge"
    ) +
  facet_wrap( ~ island) +
  theme(legend.position = "bottom")

penguins2 %>%
  group_by(species, sex, island) %>%
  summarise(body_mass_kg = mean(body_mass_kg)) %>%
  gg_col(
    x = body_mass_kg,
    y = species,
    col = sex,
    facet = island,
    position = "dodge",
    col_legend_place = "b"
  )

Other examples

storms %>% 
  group_by(year) %>% 
  summarise(wind = mean(wind, na.rm = TRUE)) %>% 
  gg_line(x = year, 
          y = wind, 
          y_zero = TRUE,
          x_labels = ~.x, 
          title = "Storm wind speed",
          subtitle = "USA average storm wind speed, 1975\u20132020", 
          y_title = "Wind speed (knots)", 
          caption = "Source: NOAA",
          theme = gg_theme(y_grid = TRUE)) +
  geom_point()

penguins2 %>% 
  gg_density(
    x = body_mass_kg, 
    col = species, 
    facet = sex, 
    col_legend_place = "b")

penguins2 %>%
  gg_jitter(
    x = species,
    y = body_mass_g,
    col = flipper_length_mm,
    col_intervals = ~santoku::chop_quantiles(.x, probs = seq(0, 1, 0.25)),
    position = position_jitter(width = 0.2, height = 0, seed = 123), 
    y_zero = TRUE)

penguins2 %>% 
  gg_smooth(
    x = bill_length_mm,
    y = flipper_length_mm,
    col = species,
    ) 

penguins2 %>%
  gg_histogram(
    x = body_mass_kg,
    col = species, 
    facet = sex, 
    col_legend_place = "b", 
    pal = pals::brewer.dark2(3))

df <- data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)

dodger <- position_dodge(width = 0.75)

gg_blank(df, x = resp, xmin = lower, xmax = upper, y = trt, col = group) +
  geom_col(position = dodger, width = 0.75, alpha = 0.9) +
  geom_errorbar(position = dodger, width = 0.2, col = "#232323")

For further information, see the ggblanket website.

simplevis: new & improved! 10 Mar 2022, 12:00 am

simplevis version 6.2.0 has arrived with tonnes of new features.

So, simplevis, if you haven’t heard of it, is a package of ggplot2 and leaflet wrapper functions.

It aims to make visualisation easier on the brain, so you can save your thinking for other stuff.

See the simplevis website for further information.

For further information, see the simplevis website.

simplevis: making leaflet sf maps 12 Jul 2021, 12:00 am

library(simplevis)
library(dplyr)
library(palmerpenguins)

Introduction

simplevis provides gglot2 (and leaflet) wrapper functions with an objective to help users make beautiful visualisation with less brainpower.

This post will discuss the leaflet wrappers that have been provided to make leaflet easier to work with. The way these functions have been designed is to follow the logic of the ggplot2 wrapper functions.

sf objects

The sf package makes it easy to work with vector data (e.g. points, lines or polygons).

sf objects have a list column called geometry on the end of the dataset, as well as some meta data describing the coordinate reference system etc.

example_point
#> Simple feature collection with 112 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1175354 ymin: 4853914 xmax: 2025939 ymax: 6096100
#> Projected CRS: NZGD2000 / New Zealand Transverse Mercator 2000
#> First 10 features:
#>      site_id median trend_category                geometry
#> 1  ARC-00001 0.0140      Improving POINT (1735609 5916179)
#> 2  ARC-00008 0.0610      Improving POINT (1753479 5976281)
#> 3  ARC-00013 0.1310      Improving POINT (1742066 5915382)
#> 4  ARC-00014 0.9900      Improving POINT (1764285 5907017)
#> 5  ARC-00015 1.0300      Improving POINT (1767401 5907336)
#> 6  ARC-00016 0.2980      Improving POINT (1768314 5908177)
#> 7  ARC-00017 0.3550      Improving POINT (1751305 5933319)
#> 8  ARC-00018 0.7350  Indeterminate POINT (1769952 5912814)
#> 9  ARC-00019 0.5000      Improving POINT (1769452 5910614)
#> 10 ARC-00026 0.1295      Improving POINT (1748608 5953465)

simplevis ggplot wrappers

Note that to create sf ggplot2 maps with simplevis:

  • Data must be an sf object
  • Data must be of POINT/MULTIPOINT, LINESTRING/MULTILINESTRING, or POLYGON/MULTIPOLYGON geometry type
  • Data must have a coordinate reference system (CRS) defined
  • No x_var and y_var variables are required
gg_sf_col(example_point,  
          col_var = trend_category, 
          borders = example_borders)

These maps can be facetted or made interactive in the same way as other ggplot2 objects with plotly::ggplotly.

The borders argument is any sf object of any sort of administrative or natural contextial borders and boundaries that you would like for your map. In the example above, a New Zealand coastline sf object has been provided to the borders argument.

simplevis leaflet wrappers

The simplevis leaflet wrappers largely work in exactly the same way.

leaf_sf_col(example_point, 
            col_var = median, 
            col_method = "quantile",
            col_cuts = seq(0, 1, 0.25),
            col_title = "Monitored medians, 2008-17")
leaf_sf_col(example_polygon, density, 
            col_method = "bin", 
            col_cuts = c(0, 10, 50, 100, 150, 200, Inf), 
            col_title = "Modelled density, 2017")

The clickable popup will default to a leafpop::popupTable of all variables, but popups can be adjusted to a subset of column using the popup_vars_vctr argument.

leaf_sf_col(example_point, 
               col_var = trend_category, 
               popup_vars_vctr = c("site_id", "median"))

The hover label will default to the colour variable, but can be adjusted using the label_var variable.

leaf_sf_col(example_point, 
               col_var = trend_category, 
               label_var = site_id)

Users have a basemap argument that defaults to “light”, but there are other options.

leaf_sf(example_point, 
           basemap = "dark")

Adding additional layers

As a leaflet object is produced, you can add additional layers with leaflet - although this may effect popups and labels.

leaf_sf_col(example_point, 
               col_var = trend_category) %>% 
  leaflet::addPolygons(data = sf::st_transform(example_borders, 4326), 
                       color = "#35B779", 
                       weight = 3, 
                       fillOpacity = 0, 
                       opacity = 1)

leaflet basemap stack for shiny

A leaflet basemap stack is available for use in shiny apps. It defaults to the top layer being “light”. You can set the bounds by adding a vector or bbox of bounds.

leaf_basemap(bounds = c(166.70047,-34.45676, 178.52966,-47.06345))

You can also specify the top layer.

library(sf)
leaf_basemap(bounds = c(166.70047,-34.45676, 178.52966,-47.06345), 
             basemap = "satellite") 

Further information

For further information, see the simplevis website.

simplevis: interactive plots with plotly::ggplotly 4 Jul 2021, 12:00 am

library(simplevis)
library(dplyr)
library(palmerpenguins)

Introduction

simplevis provides gglot2 (and leaflet) wrapper functions with an objective to help users make beautiful visualisation with less brainpower.

In the current post, we discus how to make yoursimplevis ggplot2 plots interactive html widgets using plotly::ggplotly, including how to fine-tune your tooltips.

Turning a ggplot into an interactive html widget

The plotly::ggplotly function provides the ability to convert the ggplot object to an interactive plotly html object.

Given that simplevis functions are generally ggplot2 wrapper functions that output ggplot2 objects, we can use this plotly::ggplotly function on our output object to make it interactive.

This is pretty magical, as it is just so easy - thanks plotly!

plot <- gg_point_col(penguins, 
                     x_var = bill_length_mm, 
                     y_var = body_mass_g, 
                     col_var = species)

plotly::ggplotly(plot) 

Turning off widgets other than the camera

The plotly widgets can sometimes look a bit cluttered, especially you are not using them.

Therefore, simplevis provides a plotly_camera function to turn off all widgets other than the camera.

plot <- gg_point_col(penguins, 
                     x_var = bill_length_mm, 
                     y_var = body_mass_g, 
                     col_var = species)

plotly::ggplotly(plot) %>% 
  plotly_camera()

Controlling the tooltip

Some users may want to have more or different variables in the tooltip than plotly::ggplotly defaults provide.

plotly::ggplotly provides a method for this which simplevis uses.

In the simplevis gg_* function, users can add a variable can be added to the text_var that can be used as the tooltip (for functions other than gg_density*() and gg_boxplot*()).

This variable is then used in the ggplotly tooltip when tooltip = text is added to the ggplotly function.

simplevis provides a mutate_text function which makes it easy to create a column that is a string of variable names and associated values separated by breaks for the tooltip.

This function creates a new column called text that can then be added to the text_var in the gg*() function.

If no column names are provided in a vector to mutate_text, then it defaults to providing all variable names and associated values in the dataset.

plot_data <- penguins %>% 
  mutate_text()

plot <- gg_point_col(plot_data, 
                     x_var = bill_length_mm, 
                     y_var = body_mass_g, 
                     col_var = species, 
                     text_var = text)

plotly::ggplotly(plot, tooltip = "text") %>% 
  plotly_camera()

A vector of column names can be provided to mutate_text function to get the exact arguments that the user in looking for. Note mutate_text converts column names to sentence case using the snakecase::to_sentence_case function.

plot_data <- penguins %>% 
  mutate_text(c("species", "flipper_length_mm"))

plot <- gg_point_col(plot_data, 
                     x_var = bill_length_mm, 
                     y_var = body_mass_g, 
                     col_var = species, 
                     text_var = text)

plotly::ggplotly(plot, tooltip = "text") %>% 
  plotly_camera()

You can also use this method if you were using ggplot2.

library(ggplot2)

plot_data <- penguins %>% 
  mutate_text()

plot <- ggplot(plot_data) +
  geom_point(aes(x = bill_length_mm, 
                 y = body_mass_g, 
                 col = species, 
                 text = text))

plotly::ggplotly(plot, tooltip = "text") %>% 
  plotly_camera()

Further information

For further information, see the simplevis website.

simplevis: adjusting titles and scales 27 Jun 2021, 12:00 am

library(simplevis)
library(dplyr)
library(palmerpenguins)

Introduction

simplevis provides gglot2 (and leaflet) wrapper functions with an objective to help users make beautiful visualisation with less brainpower.

In the current post, we discus how to adjust titles and scales within simplevis.

Titles

Default titles in simplevis try to provide a polished quick graph with minimal code by:

  • having no title, subtitle or caption
  • converting x, y and colour titles to sentence case using the snakecase::to_sentence_case function.
gg_point_col(penguins, bill_length_mm, body_mass_g, species)

You can add or adjust titles using the title, subtitle, x_title, y_title, col_title and caption arguments.

gg_point_col(penguins, bill_length_mm, body_mass_g, species, 
             title = "Adult penguin mass by bill length and species",
             subtitle = "Palmer station, Antarctica",
             x_title = "Bill length (mm)", 
             y_title = "Body mass (g)",
             col_title = "Penguin species",
             caption = "Source: Gorman KB, Williams TD, Fraser WR (2014)")

If you want no x, y or colour title, you need to use x_title = "", y_title = "" or col_title = "" as applicable.

Scales: consistent prefixes and the autocomplete

simplevis uses consistent prefixes in arguments to help users narrow down what they are looking for and then enable the Rstudio auto-complete to provide options.

In general, arguments that relate to:

  • the x scale start with x_
  • the y scale start with y_
  • the colour scale start with col_
  • facetting start with facet_

Therefore, if you know want to adjust the x scale but can’t think how, you can start typing x_ within the simplevis function, press tab, and then you will be presented with a lot of options. You can use the arrow keys to scroll through these, and the tab to select.

Numeric scales

simplevis graphs numeric scales default to:

  • starting from zero for numeric scales on bar graphs.
  • not starting from zero for numeric scales on all other graphs.

You can use the x_zero and y_zero arguments to change the defaults.

gg_point_col(penguins, bill_length_mm, body_mass_g, species, 
               x_zero = TRUE, 
               y_zero = TRUE)

Adjust the number of breaks for numeric x and/or y scales.

gg_point_col(penguins, bill_length_mm, body_mass_g, species, 
               y_breaks_n = 10,
               x_breaks_n = 6)

Balance a numeric scale so that it has equivalence between positive and negative values.

gg_point_col(penguins, bill_length_mm, body_mass_g, species, 
             y_balance = T)

Zero lines default on if a numeric scale includes positive and negative values, but can be turned off if desired.

gg_point_col(penguins, bill_length_mm, body_mass_g, species, 
             y_balance = T, 
             y_zero_line = F)

Discrete scales

simplevis automatically orders hbar graphs of character variables alphabetically.

plot_data <- ggplot2::diamonds %>%
  mutate(cut = as.character(cut)) %>% 
  group_by(cut) %>%
  summarise(price = mean(price)) 
  
gg_hbar(plot_data, price, cut)

If there is an inherent order to the character variable that you want it to plot in, then you should convert the variable to a factor, and give it the appropriate levels.

cut_levels <-  c("Ideal", "Premium", "Very Good", "Good", "Fair")

plot_data <- ggplot2::diamonds %>%
  mutate(cut = as.character(cut)) %>%
   mutate(cut = factor(cut, levels = cut_levels)) %>% 
  group_by(cut) %>%
  summarise(price = mean(price)) 

gg_hbar(plot_data, price, cut)

Discrete scales can be reversed easily using the relevant y_rev or x_rev argument.

plot_data <- ggplot2::diamonds %>%
  mutate(cut = as.character(cut)) %>% 
  group_by(cut) %>%
  summarise(price = mean(price)) 

gg_hbar(plot_data, price, cut,
            y_rev = TRUE)

Simple hbar and vbar plots made with gg_bar() or gg_hbar can be ordered by size using y_reorder or x_reorder. For other functions, you will need to reorder variables in the data as you wish them to be ordered.

plot_data <- ggplot2::diamonds %>%
  mutate(cut = as.character(cut)) %>% 
  group_by(cut) %>%
  summarise(price = mean(price)) 
  
gg_hbar(plot_data, price, cut, 
        y_reorder = T)

Colour scales

Customise the colour title. Note that because colour labels will be converted to sentence case by default in simplevis, but we can turn this off when we do not want this to occur using function(x) x

plot_data <- ggplot2::diamonds %>%
  group_by(cut, clarity) %>%
  summarise(average_price = mean(price))

gg_hbar_col(plot_data, average_price, cut, clarity,
            col_labels = function(x) x,  
            pal_rev = TRUE)

Reverse the palette.

plot_data <- ggplot2::diamonds %>%
  group_by(cut, clarity) %>%
  summarise(average_price = mean(price))

gg_hbar_col(plot_data, average_price, cut, clarity,
            col_labels = function(x) x, 
            pal_rev = TRUE)

Reverse the order of coloured bars.

plot_data <- ggplot2::diamonds %>%
  group_by(cut, clarity) %>%
  summarise(average_price = mean(price))

gg_hbar_col(plot_data, average_price, cut, clarity,
            col_labels = function(x) x, 
            col_rev = TRUE)

Labels

You can adjust x, y or colour scale labels using x_labels, y_labels or col_labels arguments, and functions from the scales package.

gg_point_col(penguins, bill_length_mm, body_mass_g, species, 
                   y_labels = scales::label_comma(), 
                   x_labels = scales::label_number(accuracy = 0.1))

Or via a function.

gg_point_col(penguins, bill_length_mm, body_mass_g, species, 
                   x_labels = function(x) glue::glue("{x} mm"))

Note there is a default sentence case transformation for categorical x, y or col variables. But you can use function(x) x to turn this off.

Notice with the default transformation turned off, the sex variable returns to being lower case as it is in the penguins dataset.

gg_point_col_facet(penguins, bill_length_mm, body_mass_g, sex, sex, 
  col_labels = function(x) x,
  facet_labels = function(x) stringr::str_to_upper(x)) 

NA values

You can quickly remove NA values by setting x_na_rm, y_na_rm, col_na_rm or facet_na_rm arguments to TRUE.

gg_point_col_facet(penguins, bill_length_mm, body_mass_g, sex, species,                    col_na_rm = T)

Expanding the scale

To expand the scale use x_expand and y_expand arguments with the ggplot2::expansion function, which allows to expand in either or both directions of both x and y in an additive or multiplative way.

plot_data <- storms %>%
  group_by(year) %>%
  summarise(wind = mean(wind))

gg_line(plot_data, year, wind, 
        x_expand = ggplot2::expansion(add = c(1, 2.5)),
        y_expand = ggplot2::expansion(mult = c(0.05, 0.1)))

Further information

For further information, see the simplevis website.

simplevis: working with colour 20 Jun 2021, 12:00 am

Introduction

simplevis provides gglot2 (and leaflet) wrapper functions with an objective to help users make beautiful visualisation with less brainpower.

In the current post, we will discus the simplified and consistent method for colouring that simplevis has adopted.

Overview

In simplevis, users adhere to the following rules for adjusting colour:

  1. Always define the colours to use via the pal argument (short for palette)
  2. If colouring by a variable, use a *_col() or *_col_facet() function, and define the col_var
  3. For gg_sf_col*() and gg_point_col*() functions where the col_var is numeric, also define the col_method of bin or quantile, and the col_cuts to use.
library(simplevis)
library(dplyr)
library(palmerpenguins)

1. Always define the colours to use via the pal argument

The colour palette can be changed from the default viridis colours by providing a character vector of hex codes to the pal argument.

gg_point(penguins, bill_length_mm, body_mass_g, pal = "#e7298a")

Users can get access to a large amount of colour palettes through the pals package.

2. If colouring by a variable, use a *_col() or *_col_facet() function, and define the col_var

To colour by a variable, use a *_col() function and then define that variable to be coloured using the col_var argument.

gg_point_col(penguins, bill_length_mm, body_mass_g, species)

3. For gg_sf_col*() and gg_point_col*() functions where colouring by a numeric variable, also define the col_method and col_cuts

All simplevis *_col() and *_col_facet() functions support colouring by a categorical variable.

In addition, sf and point *_col() and *_col_facet() functions support colouring by a numeric variable.

You do this by specifying whether you want to do this by:

  • defining whether the col_method is to be by bin or quantile
  • defining a vector or col_cuts. These should be between 0 and infinity (Inf) for bin and between 0 and 1 for quantile
plot_data <- ggplot2::diamonds %>% 
  slice_sample(prop = 0.01)

plot_data
#> # A tibble: 539 x 10
#>    carat cut     color clarity depth table price     x     y     z
#>    <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#>  1  0.32 Ideal   D     SI2      59.5    57   477  4.47  4.5   2.67
#>  2  1.5  Premium D     SI2      61.9    58  8649  7.34  7.27  4.52
#>  3  0.4  Premium D     VS2      62.4    59   982  4.7   4.72  2.94
#>  4  2.01 Premium I     SI2      63      59  9658  8.04  7.97  5.04
#>  5  0.51 Premium G     VS2      61.1    58  1381  5.17  5.2   3.17
#>  6  0.41 Ideal   D     VS1      61.1    56  1427  4.8   4.83  2.94
#>  7  0.31 Ideal   G     VVS2     61.9    55   707  4.33  4.36  2.69
#>  8  0.61 Ideal   H     VS2      62.5    54  1591  5.43  5.45  3.4 
#>  9  1.02 Premium G     SI2      58.1    58  4078  6.67  6.58  3.85
#> 10  0.4  Ideal   E     SI2      61.9    58   629  4.71  4.73  2.92
#> # ... with 529 more rows

gg_point_col(plot_data, 
                 x_var = carat, 
                 y_var = price, 
                 col_var = z,
                 col_method = "quantile",
                 col_cuts = c(0, 0.25, 0.5, 0.75, 1))

gg_point_col(plot_data, 
                 x_var = carat, 
                 y_var = price, 
                 col_var = z,
                 col_method = "bin",
                 col_cuts = c(0, 1, 2, 3, 4, 5, Inf))

Further information

For further information, see the simplevis website.

simplevis: visualisation made easier 7 Jun 2021, 12:00 am

Introduction

simplevis is a package of ggplot2 wrapper functions that aims to make beautiful ggplot2 visualisation with less brainpower and typing!

This blog will provide an overview of:

  • the visualisation family types that simplevis currently supports
  • how visualisation families support combinations of colouring (by a variable), facetting. both or neither.
library(simplevis)
library(dplyr)
library(palmerpenguins)

Visualisation family types

bar

plot_data <- storms %>%
  group_by(year) %>%
  summarise(wind = mean(wind))

gg_bar(plot_data, year, wind)

point

gg_point(iris, Sepal.Width, Sepal.Length)

line

plot_data <- storms %>%
  group_by(year) %>%
  summarise(wind = mean(wind))

gg_line(plot_data, year, wind)

boxplot

gg_boxplot(penguins, species, body_mass_g)

hbar (i.e horizontal bar)

plot_data <- ggplot2::diamonds %>%
  group_by(cut) %>%
  summarise(price = mean(price))

gg_hbar(plot_data, price, cut)

sf (short for simple features map)

gg_sf(example_point, borders = example_borders)

Colouring, facetting, neither or both

Each visualisation family generally has 4 functions.

The function name specifies whether or not a visualisation is to be coloured by a variable *_col(), facetted by a variable *_facet(), neither *() or both of these *_col_facet().

Colouring by a variable means that different values of a selected variable are to have different colours. Facetting means that different values of a selected variable are to have their facet.

A *() function such gg_point() requires only a dataset, an x variable and a y variable.

gg_point(penguins, bill_length_mm, body_mass_g)

A *_col() function such gg_point_col() requires only a dataset, an x variable, a y variable, and a colour variable.

gg_point_col(penguins, bill_length_mm, body_mass_g, sex)

A *_facet() function such gg_point_facet() requires only a dataset, an x variable, a y variable, and a facet variable.

gg_point_facet(penguins, bill_length_mm, body_mass_g, species)

A *_col_facet() function such gg_point_col_facet() requires only a dataset, an x variable, a y variable, a colour variable, and a facet variable.

gg_point_col_facet(penguins, bill_length_mm, body_mass_g, sex, species)

Data is generally plotted with a stat of identity, which means data is plotted as is. Only for boxplot, there is a different default stat of boxplot, which means data will be transformed to boxplot statistics. ### Further information

For further information, see the simplevis website.

About 6 May 2016, 4:48 am

This is a blog by David Hodge.

I’ve set up this blog mainly to publicise the simplevis R package via Rbloggers.