Data Driven Cheatsheets

Guest post by Jonathan Sidi

Cheatsheets are currently built and used exclusivley as a teaching tool. We want to try and change this and produce a cheat sheet that gives a roadmap to build a known product, but also is built as a function so users can input data into it to make the cheatsheet more personalized. This gives a versalility of a consistent format that people can share with each other, but has the added value of conveying a message through data driven visual changes.

Example

ggplot2 themes

The ggplot2 theme object is an amazing object you can specify nearly any part of the plot that is not conditonal on the data. What sets the theme object apart is that its structure is consistent, but the values in it change. In addition to change a theme it is a single function that too has a consistent call. The reoccuring challenge for users is to remember all the options that can be used in the theme call (there are approximately 220 unique options to calibrate at last count) or bookmark the help page for the theme and remember how you deciphered it last time.

This becomes a problem to pass all the information of the theme to someone who does not know what the values are set in your theme and attach instructions on it to let them recreate it without needing to open any web pages.

In writing the library ggedit we tried to make it easy to edit your theme so you don’t have to know too much about ggplots to make a large number of changes at once, for a quick clip see here. We had to make it easy to track those changes for people who are not versed in R, and plot.theme() was the outcome. In short think of the theme as a lot of small images that are combined to create a singel portrait.

Continue reading “Data Driven Cheatsheets”

ggedit 0.0.2: a GUI for advanced editing of ggplot2 objects

Guest post by Jonathan Sidi, Metrum Research Group

Last week the updated version of ggedit was presented in RStudio::conf2017. First, a BIG thank you to the whole RStudio team for a great conference and being so awesome to answer the insane amount of questions I had (sorry!). For a quick intro to the package see the previous post.

To install the package:

devtools::install_github("metrumresearchgroup/ggedit",subdir="ggedit")

Highlights of the updated version.

  • verbose script handling during updating in the gagdet (see video below)
  • verbose script output for updated layers and theme to parse and evaluate in console or editor
  • colourpicker control for both single colours/fills and and palletes
  • output for scale objects eg scale*grandient,scale*grandientn and scale*manual
  • verbose script output for scales eg scale*grandient,scale*grandientn and scale*manual to parse and evaluate in console or editor
  • input plot objects can have the data in the layer object and in the base object.
    • ggplot(data=iris,aes(x=Sepal.Width,y=Sepal.Length,colour=Species))+geom_point()
    • ggplot(data=iris,aes(x=Sepal.Width,y=Sepal.Length))+geom_point(aes(colour=Species))
    • ggplot()+geom_point(data=iris,aes(x=Sepal.Width,y=Sepal.Length,colour=Species))
  • plot.theme(): S3 method for class ‘theme’
    • visualizing theme objects in single output
    • visual comparison of two themes objects in single output
    • will be expanded upon in upcoming post

RStudio::conf2017 Presentation

#devtools::install_github("metrumresearchgroup/ggedit",subdir="ggedit")
rm(list=ls())
library(ggedit)
#?ggedit

p0=list(
  Scatter=iris%>%ggplot(aes(x =Sepal.Length,y=Sepal.Width))+
    geom_point(aes(colour=Species),size=6),
  
  ScatterFacet=iris%>%ggplot(aes(x =Sepal.Length,y=Sepal.Width))+
    geom_point(aes(colour=Species),size=6)+
      geom_line(linetype=2)+
    facet_wrap(~Species,scales='free')+
    labs(title='Some Title')
  )

#a=ggedit(p.in = p0,verbose = T) #run ggedit
dat_url <- paste0("https://raw.githubusercontent.com/metrumresearchgroup/ggedit/master/RstudioExampleObj.rda")
load(url(dat_url)) #pre-run example

ldply(a,names)
##                     .id      V1           V2
## 1          UpdatedPlots Scatter ScatterFacet
## 2         UpdatedLayers Scatter ScatterFacet
## 3 UpdatedLayersElements Scatter ScatterFacet
## 4     UpdatedLayerCalls Scatter ScatterFacet
## 5         updatedScales Scatter ScatterFacet
## 6    UpdatedScalesCalls Scatter ScatterFacet
## 7         UpdatedThemes Scatter ScatterFacet
## 8     UpdatedThemeCalls Scatter ScatterFacet
plot(a)

comparePlots=c(p0,a$UpdatedPlots)
names(comparePlots)[c(3:4)]=paste0(names(comparePlots)[c(3:4)],"Updated")

Initial Comparison Plot

plot(as.ggedit(comparePlots))

Apply updated theme of first plot to second plot

comparePlots$ScatterFacetNewTheme=p0$ScatterFacet+a$UpdatedThemes$Scatter

plot(as.ggedit(comparePlots[c("ScatterFacet","ScatterFacetNewTheme")]),
      plot.layout = list(list(rows=1,cols=1),list(rows=2,cols=1))
     )

#Using Remove and Replace Function ##Overlay two layers of same geom

(comparePlots$ScatterMistake=p0$Scatter+a$UpdatedLayers$ScatterFacet[[1]])

Remove

(comparePlots$ScatterNoLayer=p0$Scatter%>%
  rgg(oldGeom = 'point'))

Replace Geom_Point layer on Scatter Plot

(comparePlots$ScatterNewLayer=p0$Scatter%>%
  rgg(oldGeom = 'point',
      oldGeomIdx = 1,
      newLayer = a$UpdatedLayers$ScatterFacet[[1]]))

Remove and Replace Geom_Point layer and add the new theme

(comparePlots$ScatterNewLayerTheme=p0$Scatter%>%
  rgg(oldGeom = 'point',
      newLayer = a$UpdatedLayers$ScatterFacet[[1]])+
  a$UpdatedThemes$Scatter)

Cloning Layers

A geom_point layer

(l=p0$Scatter$layers[[1]])
## mapping: colour = Species 
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity

Clone the layer

(l1=cloneLayer(l))
## mapping: colour = Species 
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
all.equal(l,l1)
## [1] TRUE

Verbose copy of layer

(l1.txt=cloneLayer(l,verbose = T))
## [1] "geom_point(mapping=aes(colour=Species),na.rm=FALSE,size=6,data=NULL,position=\"identity\",stat=\"identity\",show.legend=NA,inherit.aes=TRUE)"

Parse the text

(l2=eval(parse(text=l1.txt)))
## mapping: colour = Species 
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
all.equal(l,l2)
## [1] TRUE

Back to our example

  #Original geom_point layer
  parse(text=cloneLayer(p0$ScatterFacet$layers[[1]],verbose = T))
## expression(geom_point(mapping = aes(colour = Species), na.rm = FALSE, 
##     size = 6, data = NULL, position = "identity", stat = "identity", 
##     show.legend = NA, inherit.aes = TRUE))
  #new Layer
  parse(text=a$UpdatedLayerCalls$ScatterFacet[[1]])
## expression(geom_point(mapping = aes(colour = Species), na.rm = FALSE, 
##     size = 3, shape = 22, fill = "#BD2020", alpha = 1, stroke = 0.5, 
##     data = NULL, position = "identity", stat = "identity", show.legend = NA, 
##     inherit.aes = TRUE))


Jonathan Sidi joined Metrum Researcg Group in 2016 after working for several years on problems in applied statistics, financial stress testing and economic forecasting in both industrial and academic settings.

To learn more about additional open-source software packages developed by Metrum Research Group please visit the Metrum website.

Contact: For questions and comments, feel free to email me at: [email protected] or open an issue in github.