Beginner's guide to R: Painless data visualization

Part 4 of our hands-on guide covers simple graphics, bar graphs and more complex charts.

1 2 3 4 5 6 7 8 Page 2
Page 2 of 8

But once you've got that down, you have a tool to create many different types of visualizations using the same basic structure.

If ggplot2 isn't installed on your system yet, install it with the command:

install.packages("ggplot2")

You only need to do this once.

To use its functions, load the ggplot2 package into your current R session -- you only need to do this once per R session -- with the library() function:

library(ggplot2)

Onto some ggplot2 examples.

ggplot2 has a "quick plot" function called qplot() that is similar to R's basic plot() function but adds some options. The basic quick plot code:

qplot(disp, mpg, data=mtcars)

generates a scatterplot.

ggplot2's quick plot function
A scatterplot from ggplot2 using the qplot() function.

The qplot default starts the y axis at a value that makes sense to R. However, you might want your y axis to start at 0 so you can better see whether changes are truly meaningful (starting a graph's y axis at your first value instead of 0 can sometimes exaggerate changes).

Use the ylim argument to manually set your lower and upper y axis limits:

qplot(disp, mpg, ylim=c(0,35), data=mtcars)

Bonus intermediate tip: Sometimes on a scatterplot you may not be sure if a point represents just one observation or multiple ones, especially if you've got data points that repeat -- such as in this example that ggplot2 creator Hadley Wickham generated with the command:

Jitter
Some scatterplots such as this don't show the full picture because one point actually represents more than one entry in your data.

qplot(cty, hwy, data=mpg)

The "jitter" geom parameter introduces just a little randomness in the point placement so you can better see multiple points:

qplot(cty, hwy, data=mpg, geom="jitter")

As you might have guessed, if there's a "quick plot" function in ggplot2 there's also a more robust, full-featured plotting function. That's called ggplot() -- yes, while the add-on package is called ggplot2, the function is ggplot() and not ggplot2().

1 2 3 4 5 6 7 8 Page 2
Page 2 of 8
Download: EMM vendor comparison chart 2019
  
Shop Tech Products at Amazon