5 min read

Comic Options for R Output

Comic Options for R Output

Comic Options for R Output

Kent Russell

2016-07-20

In this Ted Talk and his book Understanding Comics, Scott McCloud discusses his theory of iconic identification. In his theory he claims that we identify more with cartoons and icons than realistic images and words. Assuming this is true and assuming that it can be applied to output from R, some package authors have offered some packages to cartoonize R graphs. I doubt seriously that this is a complete list.

  1. xkcd
  2. phytools xkcdTree
  3. RXKCD

For even more fun, here is xkcd in Fortran.

Despite the above list, there is still not a comprehensive option in R to cartoonize images, so I thought I would try to tackle cartoonizing with comicR leveraging comic.js. This little htmlwidget is far from comprehensive, but it can apply to nearly all R graphical output.

Installation

comicR is not on CRAN, so please install with devtools::install_github() as demonstrated below.

devtools::install_github("timelyportfolio/comicR")

Base graphics

Handwritten fonts with extrafont such as Permanent Marker from Google webfonts and setting lty=2,lwd=2 gets us pretty close to a cartoon-like graph.

library(extrafont)

plot(
  1:10, type = "b", bty = "l", col="violetred3"
  , family = "Permanent Marker" # use Permanent Marker font from Google Fonts
  , lty = 2, lwd = 2            # darker and dashed lines look more comic like to me
)

You might notice though the axes are too straight for a cartoon. comicR with some SVG and comic.js magic will complete the effect. The packages SVGAnnotation, XML, and htmltools will also be very helpful in our cartoon quest. Note: currently not working in RStudio Viewer.

library(htmltools)
library(SVGAnnotation)
library(XML)
library(comicR)

tagList(
  tags$div(
    id = "comic_noconfig"
    ,tags$h3( "comicR with defaults" )
    ,HTML(
      saveXML(
        svgPlot({plot(1:10, type = "b", bty = "l", col="paleturquoise3")}, height=4, width = 6)
      )
    )
  )
  ,comicR( "#comic_noconfig" )
  ,tags$div(
    id = "comic_withconfig"
    ,tags$h3( "comicR with ff config = 3" )
    ,HTML(
      #  handle glyph id conflicts
      gsub(
        x = saveXML(
          svgPlot({plot(1:10, type = "b", bty = "l", col="plum3")}, height=4, width = 6)
        )
        , pattern = "glyph"
        , replacement = "svg2-glyph"
      )
    )
  )
  ,comicR( "#comic_withconfig", ff = 3 )
  ,tags$div(
    id = "justr-withcomic"
    ,tags$h3( "comicR + extrafont Permanent Marker" )
    ,HTML(
      #  handle glyph id conflicts
      gsub(
        x = saveXML(
          svgPlot(
            {
              plot(1:10, type = "b", bty = "l", col="chocolate3"
               , family = "Permanent Marker" # use Permanent Marker font from Google Fonts
               , lty = 2, lwd = 2            # darker and dashed lines look more comic like to me
              )
            }
            , height=4, width = 6
          )
        )
        , pattern = "glyph"
        , replacement = "svg4-glyph"
      )
    )
  )
  ,comicR( "#justr-withcomic", ff=5 )
)

comicR with defaults

{ plot(1:10, type = "b", bty = "l", col = "paleturquoise3") }

comicR with ff config = 3

{ plot(1:10, type = "b", bty = "l", col = "plum3") }

comicR + extrafont Permanent Marker

{ plot(1:10, type = "b", bty = "l", col = "chocolate3", family = "Permanent Marker", lty = 2, lwd = 2) }

Lattice graphics

library(lattice)
library(gridSVG)
library(pipeR)

dev.new( height = 10, width = 10, noRStudioGD = T )
dev.set(which = tail(dev.list(),1))
## pdf 
##   3
dotplot(variety ~ yield | year * site, data=barley)
dot_svg <- grid.export(name="")$svg
dev.off()
## png 
##   2
tagList(
  tags$div(
    id = "lattice-comic"
    ,tags$h3("lattice plot with comicR and Google font")
    ,HTML(saveXML(addCSS(
      dot_svg
      , I("svg text { font-family : Architects Daughter; }")
    )))
  )
  ,comicR( "#lattice-comic", ff = 5 )
)# %>>%

lattice plot with comicR and Google font

yield Svansota No. 462 Manchuria No. 475 Velvet Peatland Glabron No. 457 Wisconsin No. 38 Trebi 20 30 40 50 60 1932 Grand Rapids 1931 Grand Rapids Svansota No. 462 Manchuria No. 475 Velvet Peatland Glabron No. 457 Wisconsin No. 38 Trebi 1932 Duluth 1931 Duluth Svansota No. 462 Manchuria No. 475 Velvet Peatland Glabron No. 457 Wisconsin No. 38 Trebi 1932 University Farm 1931 University Farm Svansota No. 462 Manchuria No. 475 Velvet Peatland Glabron No. 457 Wisconsin No. 38 Trebi 1932 Morris 1931 Morris Svansota No. 462 Manchuria No. 475 Velvet Peatland Glabron No. 457 Wisconsin No. 38 Trebi 1932 Crookston 1931 Crookston Svansota No. 462 Manchuria No. 475 Velvet Peatland Glabron No. 457 Wisconsin No. 38 Trebi 1932 Waseca 20 30 40 50 60 1931 Waseca
#  attachDependencies(
#    htmlDependency(
#      name = "ArchitectsDaughter"
#      ,version = "0.1"
#      ,src = c(href='http://fonts.googleapis.com/css?family=Architects+Daughter')
#      ,stylesheet = ""
#    )
#  )

DiagrammeR graphics

library(DiagrammeR)
library(DiagrammeRsvg)

#  using example from DiagrammeR Readme.md
#  https://github.com/rich-iannone/DiagrammeR
grViz("
digraph boxes_and_circles {

  # a 'graph' statement
  graph [overlap = true]

  # several 'node' statements
  node [shape = box,
        fontname = Helvetica]
  A; B; C; D; E; F

  node [shape = circle,
        fixedsize = true,
        width = 0.9] // sets as circles
  1; 2; 3; 4; 5; 6; 7; 8

  # several 'edge' statements
  A->1; B->2; B->3; B->4; C->A
  1->D; E->A; 2->4; 1->5; 1->F
  E->6; 4->6; 5->7; 6->7; 3->8
}
") %>>%
  export_svg %>>%
  (
    tagList(
      tags$div(
        id = "diagrammer_example"
        ,tags$style("#diagrammer_example text {font-family:Permanent Marker;}")
        ,tags$h3("comicR + DiagrammeR")
        ,HTML(.)
        ,comicR("#diagrammer_example", ff=3)
      )
    )
  )
## pre-main prep time: 5 ms

comicR + DiagrammeR

boxes_and_circles A A 1 1 A->1 B B 2 2 B->2 3 3 B->3 4 4 B->4 C C C->A D D E E E->A 6 6 E->6 F F 1->D 1->F 5 5 1->5 2->4 8 8 3->8 4->6 7 7 5->7 6->7

Thanks

Thanks so much

  • comic.js from Balint Morvai
  • Ramnath Vaidyanathan and RStudio for htmlwidgets
  • all the contributors to R and JavaScript