Little useless-useful R functions – Looping through variable names and generating plots

Facets in ggplot2 are great for showing multiple plots on a single canvas. Assuming this usually covers many scenarios, there might be a case that you would want to save all the combinations of x and y variables in a plot as a file. Useless scenario, and again somehow useful.

Combination of Species and Petal.Width as boxplot

Given a x-variable (in this case Species) we would like to have as much as four plots, each time with different y-variable (in this case Petal.Width). So the combinations would be:

  • Species x Petal.Width
  • Species x Petal.Length
  • Species x Sepal.Width
  • Species x Sepal.Length

Creating a helper function that will take an input string and convert it to variable for boxplot:

# Helper function
Iris_plot <- function(df=iris, y) {
  ggplot(df, aes(x = Species, y = !! sym(y) )) + 
    geom_boxplot(notch = TRUE) +
    theme_classic(base_size = 10)
}

Once we have a helper function defined, loop into the datasets:

# Main loop through the columns and dataset
for(varR in variableR){
  name <- paste0(varR, "_x_Species")
  png(paste0(name, ".png"))
  print(Iris_plot(df=iris, y=varR))
  dev.off()
}

At the end, you will have in your work enviroment (check path by getwd() ) files, each holding the combination of graph.

As always, code is available in at the Github in same Useless_R_function repository.

Happy R-coding!

Tagged with: , , , ,
Posted in Uncategorized, Useless R functions
5 comments on “Little useless-useful R functions – Looping through variable names and generating plots
  1. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) […]

    Like

  2. Joe's avatar Joe says:

    Hi Tom,

    Thanks for this post,
    Great to save plots!.

    quick Q:
    How to modify the function:

    for(varR in variableR){
    name <- paste0(varR, "_x_Species")
    png(paste0(name, ".png"))
    print(Iris_plot(df=iris, y=varR))
    dev.off()
    }

    so that the plots are displayed directly in Rstudio ?,
    (not saved as files…).

    Just learning R.
    Thanks!.

    Joe
    SF
    latest R, Rstudio, Ubuntu Linux.

    Like

  3. Stuart's avatar Stuart says:

    Hello Tomaz,

    Thanks for the useless-useful tip here.

    I realised `library(rlang)` was needed to run `y = !! sym(y)` but when I ran the whole lot, this message appears:

    “Error in variableR : object ‘variableR’ not found”

    I’m also new-ish to R, so wondered if I need to substitute `variableR` with something else – or do I need another library open?

    Thanks in advance,

    Stuart

    R version 4.1.0 (2021-05-18)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 19042)

    RStudio:
    $version
    [1] ‘1.4.1717’

    $release_name
    [1] “Juliet Rose”

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

William Durkin

William Durkin a blog on SQL Server, Replication, Performance Tuning and whatever else.

$hell Your Experience !!!

As aventuras de um DBA usando o Poder do $hell

Design a site like this with WordPress.com
Get started