A very quick introduction to ggplot2

I gave a very brief 10mn introduction to ggplot2 at the Birmingham R user group meeting on Monday. The aim was to give a headstart to R users who’ve heard of ggplot2 but never got around to trying it.

I made the talk with deck.js, with a couple of css customisations. You can browse the slides here. Right and left arrows to navigate and ‘m‘ to have an overview. Most code snippets are clickable and will show the resulting plot. Click on the plot to make it disappear.

Download all the files in one go with this zip file if you’re interested. Unzip and open ggplot2Intro/introductionGGplot2/index.html with a modern browser.

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

12 Responses to A very quick introduction to ggplot2

  1. Karthik Ram says:

    Nicely done! I give deck talks all the time. Here is a pro tip. You can knit your examples with knitr from Rhtml to html, to get syntax highlighted code. Then copy the knitr generated css to a separate css file, source it at the top of your html file, and tweak it (including adding a code font) to make the whole thing extremely readable.

    Example: Most recent talk I gave.

    Even if you don’t do the above, be sure to format your code with the tidy.dir() function in the formatR package so your R code is properly styled (e.g. space on either side of an assignment operators, space after a , in a function etc).

    • CL says:

      Yes I thought of using syntax highligthing but then I wanted to highlight part of the code as well (e.g. aes(...)). But your slides look really good I have to say.
      As for adding space before and after an assignment, I know it’s recommended and widespread but I’m afraid it comes to personal preferences and it’s something I never do, in any language I code in. I find the text too ‘spaced out‘ visually and more an hindrance than a help.


  2. David Onder says:

    Unfortunately, I am using the modern browser on my iPad and this does not display correctly.

    :( David

    • CL says:

      hmm that’s interesting. Do you have any problem with these slides: http://imakewebthings.com/deck.js/ or Karthik’s talk (from his comment) ?
      When you say it doesn’t display correctly, what do you mean exactly? I can’t test it myself, not having an iPad.

      Thanks for your interest.

    • Karthik Ram says:

      It does display fine on ios devices but the challenge is that navigation is keyboard driven (with left and right arrow keys) and the on screen keyboard does not show up unless you’re at a text input box.

      The left and right buttons show up hover, but you can’t hover on a ipad. But if you use do swipe to the left / right of the screen, it will eventually switch slides (just not as seamless as you’d experience on a computer). It’s on my list of things to contribute to the Deck library eventually (a better navigation system for mobile deck presentation).

  3. Bill Raynor says:

    Your ggplot code does not work. Is there something missing from the slides?
    I’m using ggplot2 on R 2.15.1 on a Windows 7 machine.

    > ggplot(small)
    Error: No layers in plot
    > +geom_point(aes(x=carat,y=price,colour=cut))
    Error in +geom_point(aes(x = carat, y = price, colour = cut)) :
    invalid argument to unary operator
    > +scale_y_log10()
    Error in +scale_y_log10() : invalid argument to unary operator
    > +facet_wrap(~cut)
    Error in +facet_wrap(~cut) : invalid argument to unary operator
    > +ggtitle(“First example”)
    Error in +ggtitle(“First example”) : invalid argument to unary operator

    • CL says:

      What’s missing is me explaining this line :)

      Either type it in one line, with no line-breaks, from the command line. i.e.

      Select All Code:
      ggplot(small)+geom_point(aes(x=carat,y=price,colour=cut))+scale_y_log10()+facet_wrap(~cut)+ggtitle("First example")

      Or store the ggplot2 object in a variable and build it bit by bit, and then display it with print():

      Select All Code:
      p <- ggplot(small)
      p <- p+geom_point(aes(x=carat,y=price,colour=cut))
      p <- p+scale_y_log10()+facet_wrap(~cut)+ggtitle("First example")
  4. Bill Raynor says:

    That was a fast response! Thank you. I was coming back to say I figured it out after looking at the examples later.
    1. your code works just fine in the RStudio editor.
    2. Unfortunately, I was using the console. (Hey, I’m new to R!) The following code works in the console:

    > ggplot(small)+
    + geom_point(aes(x=carat,y=price,colour=cut))+
    + scale_y_log10()+
    + facet_wrap(~cut)+
    + ggtitle(“First example”)

    Thanks for the example and the response. I took a data visualization short course from Hadley Wickham at JSM this year, but he went so fast I missed much of it. It wasn’t designed as a “work along with Hadley” session for R newbies…

    • CL says:

      True, it works if you break the line after an operator, I had forgotten about this.
      I’m glad to hear it’s useful, thank you!

  5. mikefantast says:

    Thanks, very nicely done.

Leave a Reply

Your email address will not be published. Required fields are marked *


− two = 3

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">