9  Testing your skills: ggplot

Author

Assignment, Dr. Sara Hermann. Code, Jared Adam

Recreate these plots to the best of your ability

In this section, we will be using the mtcars data set. Let’s start by reading in ggplot and the mtcars data set.

library(ggplot2)
data("mtcars")

For more information about this data set, look in the help section.

?mtcars()

Now, let’s take a look a little closer at the data set. Which ‘class’ do these variables belong to? Are they numeric? Characters? Integers?

This knowledge is very impoartant when building your plots.

View(mtcars)
str(mtcars)

Hints

Here is an example of how to change the variable type inside of the ggplot lines and outside. Notice here, we are calling the am column with the $ operator.

ggplot(mtcars, aes(x = as.factor(am)))
mtcars$am <- as.factor(mtcars$am)

You may also need to search for ggplot format help on these. Please see the extra resources at the bottom of this page.

Plot time

Graph 1: geom_point

Graph 2: geom_boxplot

[1] "0" "1"

More to come!! 6/9/2023

Additional resources

Lecture notes

Basic R graphics guide

ggplot online textbook