2 y-axis plotting

A simple plotting feature we need to be able to do with R is make a 2 y-axis plot. First let’s grab some data using the built-in beaver1 and beaver2 datasets within R. Go ahead and take a look at the data by typing it into R as I have below.

# Get the beaver datasets
beaver1
beaver2

We’re going to plot the temperatures within both of these datasets, which we can see (after punching into R) is the third column.

First let’s check the length of these datasets and make sure they’re the same.

# Get the length of column 3
length(beaver1[,3])
length(beaver2[,3])

[1] 114
[2] 100

Since beaver1 is longer, we’ll only plot rows 1 through 100 of the temperature data, so that it is the same length as beaver2.

# Plot the data
plot(beaver1[1:100, 3], type ="l", ylab = "beaver1 temperature")

Cool, your plot should look like this.
beaver1

Now, let’s add that second dataset on the right y-axis. So, we have to have to create a plot on top of this plot using the command par(new = TRUE).

# Add the second y-axis
plot(beaver1[1:100, 3], type ="l", ylab = "beaver1 temperature")
par(new = TRUE)
plot(beaver2[,3], type = "l")

beaver1-2

Woah, this plot is ugly! We have 2 y-axis labels plotting, 2 y-axis values plotting, and 2 x-axis values and labels plotting. Let’s turn those off using the commands xaxt = “n” and yaxt = “n”.

# updated plot
plot(beaver1[1:100, 3], type ="l", ylab = "beaver1 temperature")
par(new = TRUE)
plot(beaver2[,3], type = "l", xaxt = "n", yaxt = "n",
     ylab = "", xlab = "")

beaver1-3

Okay, it’s still pretty ugly, so let’s clean it up. Let’s make the margins bigger on the right side of the plot, add a y2 axis label, add a title, change the color of the lines and adjust the x-axis label. Don’t forget the legend! Here’s the code:

# final plot
par(mar = c(5, 5, 3, 5))
plot(beaver1[1:100, 3], type ="l", ylab = "beaver1 temperature",
     main = "Beaver Temperature Plot", xlab = "Time",
     col = "blue")
par(new = TRUE)
plot(beaver2[,3], type = "l", xaxt = "n", yaxt = "n",
     ylab = "", xlab = "", col = "red", lty = 2)
axis(side = 4)
mtext("beaver2 temperature", side = 4, line = 3)
legend("topleft", c("beaver1", "beaver2"),
       col = c("blue", "red"), lty = c(1, 2))

beaver2

Woo! Looks good. That’s all for now.

Advertisements

ggplot2 (ggplot) Introduction

In this post I’ll briefly introduce how to use ggplot2 (ggplot), which by default makes nicer looking plots than the standard R plotting functions.

The first thing to know is that ggplot requires data frames work properly. It is an entirely different framework from the standard plotting functions in R. Let’s grab a default data frame in R called mtcars. Let’s confirm it’s a data frame using some code:

# Get the mtcars data types
class(mtcars)

R confirms that this is in fact a data frame.

# the output
[1] "data.frame"

Feel free to take a look at the data itself by just typing the name into R. For bevity, I won’t show the data in this post.

# Look at mtcars
mtcars

Next let’s define some standard plot function names in ggplot.
geom_point = scatterplot (points or solid lines)
geom_boxplot = boxplot
geom_bar = column plot
There’s many more (really cool) plot types, but I’ll stop here for now.

Let’s make our scatterplot. Here’s the code to make a standard plot. Don’t forget to load the package ggplot2 before running this code using the library function (install ggplot2 first if you haven’t done so before).

# Plot the data
library(ggplot2)
ggplot(mtcars, aes(hp, mpg)) + geom_point()

basic

Success! The code above seems strange at first, but let’s dive into how it works. First we call ggplot and provide the data frame name ‘mtcars’. Then we give the x & y variables using the aes command. Finally we specify we’re making a scatterplot by attaching + geom_point().

Now let’s make this look better! This is where the power of ggplot shines. It’s really easy to make a nice looking plot.

# Plot the data
p <- ggplot(mtcars, aes(hp, mpg))
p + geom_point() + labs (x = "Horsepower (hp)", y = "Miles per Gallon (mpg)") +
  ggtitle("My mtcars Plot")

basic2

We can see that the syntax is a bit different this time. We save the first ggplot call to a variable p (p for plot), but any variable will work. Then we attached more plotting features using p + ——. For this plot we added custom x and y axis labels and a title.

Next let’s make a change to the overall look of the plot, using what ggplot calls a theme. We’ll add theme_bw.

# Plot the data
p <- ggplot(mtcars, aes(hp, mpg))
p + geom_point() + labs (x = "Horsepower (hp)", y = "Miles per Gallon (mpg)") +
  ggtitle("My mtcars Plot") + theme_bw()

basic3

Finally, let’s spruce it up my coloring the points blue and making them bigger, while also making our axes and titles bigger. The code below makes this final plot.

# Make the final plot
p <- ggplot(mtcars, aes(hp, mpg))
p + geom_point(size = 3, color = "blue") + 
  labs (x = "Horsepower (hp)", y = "Miles per Gallon (mpg)") +
  ggtitle("My mtcars Plot") + theme_bw()+
  theme(axis.text = element_text(size = 12), 
        axis.title = element_text(size = 14),
        plot.title = element_text(size = 18, face = "bold"))

ggplot2 final

Hope this helped explain the basics of ggplot. Here’s the link to the ggplot2 documentation (click me).