R-Studio ggplot2 Tutorial

I’ll be providing a tutorial on a core aspect of R programming: the ggplot2 package. The ggplot2 package is a package in R that allows a user to create different data visualizations based on their wants and needs. It can seem a little intimidating at first but it is a surprisingly simple tool that can be used to create increasingly complex models and charts. This could be extremely useful for the digital arts and humanities as R allows you to create, manipulate, and visualize your data easily, all in the same application. So let’s get to the tutorial.

Step 1. Load your packages

The first step to using any R tool is loading the package. It’s pretty simple: Install the package and then load into R. You can load either the ggplot2 package separately or load the tidyverse package, which includes ggplot2 and many other packages.

image of library R code
Library command

Step 2. Load your data

The next step in using ggplot is to load your data. There’s a couple different ways to do that. Most times will require you to load a csv file using the read_csv function but for this tutorial I’ll be using the cars data set that comes in the tidyverse package. In order to view the cars data, we’ll use the View command.

image of View command
View command
Image of cars data set.
cars data set

Step 3. Choose your geom

An important part of using ggplot2 is deciding on what type of visualization you want to use. A couple of deciding factors would be the number of variables in your data set, the type of variables in your data set, what you want to visualize, etc. For this data set, with two continuous and numerical variables, a scatterplot is the best plot to use.

Step 4. Creating the plot

The second-to-last step is to actually create the plot. It’s a pretty simple process that only involves a few lines of code. When you create your lines of code, there are a two different approaches about where certain code goes, specifically the aesthetics or aes command, but it’s honestly just a matter of preference.

Image of ggplot code.
ggplot code
Scatterplot output of ggplot code
Scatterplot of cars data set

Step 5. Edit your plot

The final step is to now edit your plot to your liking. Add some color, change the theme, change axis labels, etc. All things that can be easily done in R. I’ll show you how to do those things in this tutorial. You will need to use the theme command to change the theme, add a color= command to change the color, and add a labs command to change axis labels. I’ll also add a linear regression line to the plot.

Additional code edit for ggplot
Additional code to edit your plot
Final ggplot  output
Final ggplot product

Additional Resources

I highly recommend the text R For Data Science as it provides a lot of extremely useful help for different r commands and packages. I also recommend Data Science: A First Introduction.

3 thoughts on “R-Studio ggplot2 Tutorial

  1. This is a really cool and in-depth tutorial. I had never thought of doing something like this, but now I might try. I really liked the detail that you added to each of your steps, it made your tutorial quite easy to follow.

  2. I really liked how intuitive you have made this tutorial to be. You have really showed the efficiency of R at data visualization, though I would like to know how to use that read_csv function you mentioned, as well as if you have any recommendation of a good place to learn R.

  3. As someone who just learned about r this term, I think you did a fantastic job in explaining how everything works. However, for those that have no previous experience, your tutorial might be a bit unclear, for example you did not show how to read data or do numeric statistical calculations.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

css.php