

In contrast, here’s how to get the same breakdown in base graphics.Ĭurrdata <- tips & tips$sex = sexes,]
#RSTUDIO GGPLOT CODE#
The code is almost the same as the previous ggplot2 snippet. The ease of use is more evident when you facet on multiple categorical variables. Plot(currdata$total_bill, currdata$tip/currdata$total_bill, Base requires that you use a for loop, subset on the sexes, and then call plot() for each iteration. It’s not difficult to make this with base graphics, but it’s not as straightforward. Sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) Going back to the tips data, here’s how to create a scatterplot for each sex.
#RSTUDIO GGPLOT SERIES#
For example, before I made the interactive version of a time series chart on marrying age, I looked at all the demographic breakdowns in R.įacets with ggplot2 are pretty straightforward using facet_grid() and a common notation for R users. During exploratory data analysis, you often need to create graphs of various categories. This is by far the most useful part of ggplot2, and if I use the package again it will be for facets. But for this super basic stuff, we’re looking at the same amount of work so far. I prefer to handle all of my data outside of any visualization calls, so I’m okay with this. With ggplot2, you prepare the data in a similar fashion, before the ggplot() call: Order how you want and pass the result to the function. In base graphics, you work outside the barplot() function. You get a similar bar chart to the one above.įor this example, I’d order the bars by time though - Thursday through Sunday - instead of order of appearance in the data frame.

In this case, you can use table() to aggregate by day, and you pass that result to barplot(). However, in base graphics, you work with the data outside of the visualization functions. This gives you a bar chart where the height shows the number of tips per day. In ggplot2, you specify a binning by day through aes() and geom_bar(). Say you have a data frame of tips at a restaurant (from the reshape2 package): Ggplot2 also has some built-in data management. Whereas the single function call to barplot() is specialized to one thing. The idea is that you can piece together various parts using the grammar for other visualization types. On the other hand, you use the barplot() function with base graphics and specify everything in the function arguments. There is a call for each component, and you piece them together with the + operator. This is how you make any chart with ggplot2. When you put it all together, you get a complete chart. The basic idea is that you can split a chart into graphical objects - data, scale, coordinate system, and annotation - and think about them separately. If you’re unfamiliar with ggplot2, it implements a “grammar of graphics” based on Leland Wilkinson’s book The Grammar of Graphics. More importantly, look closer at the code for each. I’ll go more into looks later, but for now, let’s just imagine this is how it always is. When we think about graphics from either side, we imagine these aesthetics, and it’s how you can spot one or the other. The base graphics bar chart is more barebones. The tick labels are smaller than the axis labels and a light gray. The ggplot2 bar graph has the now familiar gray background and white grid lines. Here are the graphics and code that I got and what I learned. So instead, I worked through Winston Chang’s abridged R Graphics Cookbook and translated the ggplot2 examples to base graphics in the process.

The problem is that I don’t use the package, making any comparison useless. It seemed like a good time to revisit ggplot2 to make my own comparison. Then David Robinson rebutted with why ggplot2 is superior to R’s lowly base graphics. However, last month, Jeff Leek explained why he purposely avoids ggplot2. It’s just that base graphics continues to get me where I want to go, and the times I tried ggplot2, it didn’t get me anywhere faster than the alternative. It’s not that I think one is better than the other. These days, people tend to either go by way of base graphics or with ggplot2. Although there are many packages, ggplot2 by Hadley Wickham is by far the most popular. Then there are R packages that extend functionality. R comes with built-in functionality for charts and graphs, typically referred to as base graphics. In R, the open source statistical computing language, there are a lot of ways to do the same thing.
