A Beginner’s Guide to ggplot2

A Beginner’s Guide to ggplot2

If you’re attending ODSC West and would like to learn (or extend your knowledge) of data visualization, please attend our workshop on.

Below are some questions we received from attendees about our ODSC workshop.

is a graphing syntax that accurately “describes the properties of a plotting system.” These properties include:

Install from CRAN or you can use the development version found on the package website

We’ll use the dataset provided by the package by Alison Hill, Alison Hill, and Kristen Gorman.

graphs are built in layers, and they all start with a argument (in this case, it’s ).

Once we have an initialized plot, we’re ready to start mapping our graph aesthetics with (i.e. providing variables and their locations). Let’s put on the and on the .

The graph above has 1) , and 2) variables ( and ). The third step is to add a geom (or geometric object), which is the type of plot that we want to create. In this case, we’ll add (for points, or a scatter-plot).

In three lines of code, we’ve created a scatter-plot. We’ve also used the basic template for creating graphs with :

While this graph might not be ready for publication, it is infinitely extensible because it was built using s grammar.

A language is considered functional when it’s capable of “making infinite use of finite means”. does this by providing an infinite number of potential graphs from a finite number of functions. Consider the graph we created above with three lines of code. We can add more aesthetics (with ) to highlight the differences between groups for the and variables.

We can also include more geoms to further illustrate the group differences (with .

As you can see, with relatively few lines of code, we’re able to quickly iterate through versions of a graph. also gives us incredible levels of control over how graphs are displayed. For example, we can remove the legend and use facets to separate each group into a small-multiples.

We can add finishing touches with labels and themes.

The consistent syntax and underlying philosophy of s grammar allow us to quickly generate new graphs (and make adjustments to existing graphs).

There are also 100+ extensions, and this number is still growing. Extensions include additional geoms (like ) and themes (like ).

If you understand the grammar, extensions are like plug-and-play features to for graphs. We simply adapt our template for the new geom and theme layers…

…and we have a new graph!

We hope you’ll come join us for the workshop! You’ll walk away with a solid introduction and lots of code examples to take home and tinker with.

This can be confusing to new R users, especially if they’ve been using the pipe () from the package. The pipe allows us to easily pass the output from a function on the left as an input to the function on the right (in a ‘pipeline’). However, graph layers are added using the plus symbol (). Hadley Wickham touches on the background for why it was implemented this way in this interview, “I think I was reading about operator overloading and I thought “Oh maybe I could do this with ‘+’ instead”, and it kind of makes sense, you know, because you’re adding layers to the plot”

This website contains a gallery of extensions for . It’s always a good idea to check on Twitter, too.

has a free online book and package website with loads of examples.

Martin Frigaard is a Senior Clinical Programmer at BioMarin, where he builds dashboards and tools for making data-informed decisions. Previously, Martin built statistical tools and dashboards for the Diabetes Technology Society, a contributing author for Data Journalism in R on the Northeastern University School of Journalism blog/website, and other volunteer and non-profit organizations. He’s a data journalism instructor for California State University, Chico. Martin holds a graduate degree in Clinical Research and is passionate about data literacy and open source technologies.

Peter Spangler is a hands-on data science leader with a business-focused approach to building data science solutions and telling stories with data. Experienced in translating business problems into data products using advanced statistical techniques and ML to support decision-making in a variety of rapid growth environments. Scaled data science solutions for user acquisition, retention, channel optimization, revenue, and fraud at Lyft, Alibaba, and Citrix. Currently leading Marketing Science for Growth at Nextdoor.

Data visualization is a powerful tool for facilitating confident, informed decision-making. ggplot2 is one of the most popular data visualization packages in use today. Based on comprehensive grammar and syntax, ggplot2 gives you the ability to create data visualizations quickly and iteratively, whether it’s a simple bar-chart or a complicated network analysis.

This workshop will teach you how to manipulate and structure your data for visualizations, graph elements, and their associated terminology, how to select the appropriate graph based on your data, and how to avoid common graphing mistakes. You will also learn how to customize data visualizations and give them the ‘personal touches’ that make them memorable to your audience.

Images Powered by Shutterstock