Overview

This is a brief tutorial on how to do basic plotting in R. The idea is to take more of a lead by example approach. So, ideally you should try to understand the code presented here, copy and paste it directly into R, and then adapt it to work with your data. You can directly download the r script with all of the examples here.

Basic set up

First, we will create some fake data that we will use for our example plots in base R, lattice, and ggplot2.

set.seed(1)
vot <- rnorm(100,15,5)
vot <- sort(vot, decreasing=FALSE)
phon <- "t"
stress <- c(rep("stressed",50),rep("unstressed",50))

df1 <- data.frame(phon,stress,vot)

vot <- rnorm(100,-60,15)
vot <- sort(vot, decreasing=FALSE)
phon <- "d"
df2 <- data.frame(phon,stress,vot)

df <- rbind(df1, df2)
summary(df)
##  phon           stress         vot        
##  t:100   stressed  :100   Min.   :-88.72  
##  d:100   unstressed:100   1st Qu.:-62.66  
##                           Median :-10.73  
##                           Mean   :-22.51  
##                           3rd Qu.: 15.47  
##                           Max.   : 27.01

Looks good. Now we are ready to get started. R has distinct graphing systems. What you see on the internet and in most journals is pretty much always one of the following:

We will start with base R and move down the list. I will try to explain the idea behind the syntax and point out how they differ from each other.

Base R

Will will start by looking at different combinations of factors from our dataset. In each case, the code will be shown as x ~ y, which should be read as x as a function of y. So our first example, plot(vot ~ phon, data = df), means plot the dependent variable vot as a function of the factor phon (/t/ and /d/). Remember data = df tells R that the variables we are referring to come from the dataframe df. This is what it looks like:

plot(vot ~ phon, data = df)

Clearly these to sounds have different VOT. Cool! If you are not interested in specifying the dataframe, you don’t have to. You can use the $ to call the dataframe at the same time you call the variables. For example, instead of vot ~ phon, I could have used df$vot ~ df$phon. (note: you should probably just specify the dataframe because it’s good practice). Now let’s visualize other combinations of our variables.

plot(df$stress, df$vot)

Notice a few things here. I called the dataframe with each variable using $, and I did not use the ~, but rather a comma ,. This is equivalent to telling R “Hey, plot this on the x axis, and this then on the y axis”. R then assumes that the two variables can be plotted indepently of each other, but obviously we are interested in looking for some kind of relationship. In this case, it looks like there may be a main effect of stress. Let’s try another.

plot(df$vot, df$stress)

Since we are plotting voice-onset time on the x-axis and a categorical factor on the y-axis, we are seeing where each VOT point lies on a horizontal plain plotted in miliseconds. This could be useful for plotting a logistic regression model.

Using the par function

As we’ve seen, base R can give us some nifty graphs without too much work, but sometimes we need to tweak things to fit out needs. Here are some of the things we can adjust.

  • pch: the plotting symbol (default is open circle)
  • lty: the line type (default is solid line), can be dashed, dotted, etc.
  • lwd: the line width, specified as an integer multiple
  • col: the plotting color, specified as a number, string, or hex code; the colors function gives you a vector of colors by name
  • las: the orientation of the axis labels on the plot
  • bg: the background color
  • mar: the margin size
  • oma: the outer margin size (default is 0 for all sides)
  • mfrow: number of plots per row, column (plots are filled row-wise)
  • mfcol: number of plots per row, column (plots are filled column-wise)

Other functions

We can also make other adjustments.

  • plot: make a scatterplot, or other type of plot depending on the class of the object being plotted
  • lines: add lines to a plot, given a vector x values and a corresponding vector of y values (or a 2-column matrix); this function just connects the dots
  • points: add points to a plot
  • text: add text labels to a plot using specified x, y coordinates
  • title: add annotations to x, y axis labels, title, subtitle, outer margin
  • mtext: add arbitrary text to the margins (inner or outer) of the plot
  • axis: adding axis ticks/labels

Here is an example of adjusting the mfrow. This can be used to make side-by-side plots.

par(mfrow = c(1, 2)) # 1 row, 2 cols

plot(as.numeric(df$phon), df$vot,  axes = F, type = "n",
     ylim = c(-100, 30), xlim = c(0.8, 2.20), ylab = "vot", 
     xlab = "phon", main = "") # base plot, no data

box() # add box
axis(side = 1, at = (1:2), labels = c("/t/", "/d/")) # add x axis
axis(side = 2,las = 1, at = seq(-100, 30, by = 20)) # add y axis
#legend("top", legend=c("/t/", "/d/"), pch=c("1","2"), col=c("blue","red")) # add legend
points(df$phon[df$phon == "t"], df$vot[df$phon == "t"], col = "blue", cex = 2) # add data /t/
points(df$phon[df$phon == "d"], df$vot[df$phon == "d"], col = "red", cex = 2) # add data /d/

plot(vot ~ phon, data = df) # add second plot

Looks good, but we still can’t see how stress interacts with the VOT of each phoneme. Normally in our models in R we use * to indicate and interaction, but this doesn’t seem to be the best solution…

plot(vot ~ phon * stress, data = df) # this isn't ideal

Let’s take a look at lattice to see how we can take care of this.

Lattice

library(lattice)
bwplot(vot ~ phon | stress, data = df)

Other functions

  • xyplot: this is the main function for creating scatterplots
  • bwplot: box-and-whiskers plots (“boxplots”)
  • histogram: histograms
  • stripplot: like a boxplot but with actual points
  • dotplot: plot dots on “violin strings”
  • splom: scatterplot matrix; like pairs in base graphics system
  • levelplot, contourplot: for plotting “image” data
histogram(vot)

This looks familiar

stripplot(vot ~ phon, data = df) 

Similar to above, good for visualizing interactions

dotplot(vot ~ stress, data = df) 

More examples | with cooler functions

create fake data

x <- rnorm(100)
y <- x + rnorm(100, sd = 0.5)
f <- gl(2, 50, labels = c("Group 1", "Group 2"))

Plot 1

xyplot(y ~ x | f)

Plot 2

xyplot(y ~ x | f,
       panel = function(x, y, ...) {
               panel.xyplot(x, y, ...)
               panel.lmline(x, y, col = 2)
})

ggplot2

Works with a data frame

Examples

  • install package if you don’t have it

install.packages(ggplot2)

Basic plot = qplot()

  • Works like the plot function in base
  • Looks for data in a data frame, like lattice
  • Plots are made up of aesthetcs (size, shape, color) and geoms (points, lines)

Looks familiar *structure = x cord, y cord, dframe

library(ggplot2)
## Loading required package: methods
qplot(phon, vot, data = df) 

Add stress by changing color of points

qplot(phon, vot, data = df, color = stress) 

You can add mead and sd (diff data)

qplot(displ, hwy, data = mpg, geom = c("point", "smooth")) 
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

Some historgram examples

qplot(vot, data = df, fill = phon) # histogram
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

qplot(vot, data = df, fill = stress) # historgram
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

qplot(vot, data = df, geom = "density") # kernal density

qplot(vot, data = df, geom = "density", color=phon) # kernal density

qplot(log(vot), data = df, geom = "density", color=stress) # kernal density with transform

Facets (like | in lattice)

qplot(phon, vot, data = df, facets = . ~ stress)

qplot(vot, data = df, facets = stress ~ ., binwidth = 3)

ggplot()

  • is the core function
  • does things qplot() cant
g <- ggplot(df, aes(phon, vot)) 

Above code wont work, we must add layers

g + geom_point()

g + geom_point(aes(color = stress), size = 4, alpha = 1/4)

We can see some more cool funtions if we add a column to our df…

var1 <- rnorm(200,200,20)
var1 <- data.frame(var1)
df2 <- cbind(df,var1)
g2 <- ggplot(df2, aes(var1, vot))
g2 + geom_point(aes(color = phon), size = 4, alpha = 1/2) +
   facet_wrap(~ stress)

Save plots

pdf(file="fileName.pdf")
# put plot code here
dev.off()

More info

Identification sigmoid example

  • This is an example of how to get the sigmoid “s-curve” that we see in perception experiments
  • First we need to add some data
  • This was taken from the identification experiment in Casillas (2013)
ident <- structure(list(participant = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 
23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 
24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 
26L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 28L, 
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 30L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L, 30L), group = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1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 4L, 5L, 6L, 7L, 
    8L, 9L, 10L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
    0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 0L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 0L, 1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L, 10L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
    9L, 10L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 0L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), dpro = c(0.62, 
    0.62, 0.57, 0.57, 0.57, 0.53, 0.48, 0.49, 0.44, 0.37, 0.33, 
    0.62, 0.6, 0.46, 0.48, 0.45, 0.44, 0.42, 0.39, 0.35, 0.36, 
    0.28, 0.7, 0.65, 0.7, 0.65, 0.6, 0.55, 0.56, 0.55, 0.52, 
    0.43, 0.43, 0.66, 0.59, 0.53, 0.52, 0.53, 0.47, 0.48, 0.41, 
    0.43, 0.37, 0.33, 0.72, 0.63, 0.51, 0.51, 0.57, 0.47, 0.53, 
    0.49, 0.46, 0.41, 0.33, 0.56, 0.63, 0.58, 0.57, 0.51, 0.44, 
    0.4, 0.38, 0.36, 0.32, 0.3, 0.62, 0.59, 0.53, 0.53, 0.53, 
    0.48, 0.42, 0.44, 0.47, 0.33, 0.38, 0.63, 0.61, 0.56, 0.56, 
    0.49, 0.45, 0.48, 0.42, 0.37, 0.39, 0.33, 0.58, 0.59, 0.57, 
    0.58, 0.58, 0.53, 0.42, 0.45, 0.38, 0.23, 0.28, 0.56, 0.51, 
    0.52, 0.49, 0.47, 0.46, 0.43, 0.4, 0.37, 0.33, 0.29, 0.49, 
    0.51, 0.49, 0.48, 0.51, 0.52, 0.52, 0.48, 0.45, 0.52, 0.48, 
    0.36, 0.4, 0.38, 0.45, 0.34, 0.32, 0.31, 0.31, 0.31, 0.29, 
    0.29, 0.53, 0.51, 0.43, 0.48, 0.52, 0.47, 0.39, 0.47, 0.42, 
    0.32, 0.32, 0.48, 0.51, 0.45, 0.49, 0.39, 0.53, 0.36, 0.44, 
    0.39, 0.43, 0.36, 0.56, 0.56, 0.44, 0.48, 0.43, 0.51, 0.45, 
    0.44, 0.49, 0.52, 0.42, 0.68, 0.66, 0.66, 0.56, 0.57, 0.55, 
    0.62, 0.56, 0.52, 0.57, 0.53, 0.57, 0.57, 0.56, 0.56, 0.45, 
    0.49, 0.59, 0.43, 0.47, 0.52, 0.45, 0.64, 0.62, 0.53, 0.51, 
    0.49, 0.48, 0.47, 0.4, 0.43, 0.45, 0.35, 0.61, 0.6, 0.57, 
    0.47, 0.53, 0.53, 0.49, 0.45, 0.52, 0.52, 0.52, 0.53, 0.53, 
    0.51, 0.58, 0.57, 0.53, 0.53, 0.49, 0.45, 0.45, 0.44, 0.91, 
    0.88, 0.78, 0.55, 0.57, 0.49, 0.52, 0.48, 0.33, 0.24, 0.22, 
    0.86, 0.84, 0.78, 0.65, 0.7, 0.52, 0.45, 0.45, 0.32, 0.23, 
    0.21, 0.73, 0.73, 0.68, 0.64, 0.64, 0.51, 0.46, 0.42, 0.33, 
    0.28, 0.21, 0.81, 0.79, 0.71, 0.65, 0.65, 0.5, 0.59, 0.45, 
    0.34, 0.32, 0.26, 0.76, 0.68, 0.63, 0.44, 0.29, 0.32, 0.23, 
    0.25, 0.16, 0.18, 0.12, 0.75, 0.73, 0.67, 0.55, 0.51, 0.44, 
    0.32, 0.25, 0.16, 0.12, 0.13, 0.74, 0.73, 0.73, 0.56, 0.29, 
    0.32, 0.23, 0.25, 0.16, 0.12, 0.19, 0.81, 0.78, 0.68, 0.53, 
    0.33, 0.44, 0.23, 0.25, 0.16, 0.13, 0.19, 0.71, 0.68, 0.66, 
    0.54, 0.43, 0.32, 0.27, 0.25, 0.16, 0.2, 0.11, 0.79, 0.72, 
    0.69, 0.56, 0.44, 0.32, 0.18, 0.15, 0.19, 0.12, 0.14), drt = c(1.19, 
    1.12, 1.15, 1.13, 1.09, 1.16, 1.07, 1.06, 1.1, 1.07, 1.05, 
    0.88, 0.86, 0.9, 0.86, 0.94, 0.9, 0.89, 0.84, 0.81, 0.88, 
    0.91, 1.25, 1.22, 1.28, 1.29, 1.24, 1.23, 1.25, 1.21, 1.23, 
    1.19, 1.21, 0.93, 0.84, 0.9, 0.94, 0.93, 0.95, 0.9, 0.9, 
    0.92, 0.91, 0.94, 0.93, 0.91, 0.89, 0.86, 0.92, 0.92, 0.88, 
    0.87, 0.88, 0.89, 0.87, 0.9, 0.91, 0.87, 0.84, 0.86, 0.87, 
    0.92, 0.9, 0.85, 0.83, 0.9, 0.76, 0.78, 0.75, 0.74, 0.77, 
    0.74, 0.73, 0.81, 0.76, 0.75, 0.78, 0.61, 0.62, 0.61, 0.61, 
    0.59, 0.62, 0.6, 0.62, 0.6, 0.64, 0.64, 0.99, 0.96, 1.05, 
    1.01, 0.95, 0.98, 1, 1.02, 0.98, 1.02, 1.02, 0.97, 0.98, 
    0.99, 0.97, 0.99, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.67, 
    0.66, 0.67, 0.64, 0.65, 0.65, 0.64, 0.65, 0.68, 0.69, 0.64, 
    0.83, 0.83, 0.83, 0.82, 0.77, 0.82, 0.79, 0.8, 0.83, 0.75, 
    0.76, 0.93, 0.89, 0.89, 0.9, 0.9, 0.92, 0.93, 0.87, 0.88, 
    0.83, 0.8, 0.71, 0.71, 0.72, 0.7, 0.75, 0.71, 0.71, 0.7, 
    0.73, 0.75, 0.71, 0.77, 0.73, 0.7, 0.71, 0.7, 0.7, 0.72, 
    0.68, 0.69, 0.67, 0.69, 0.66, 0.65, 0.63, 0.65, 0.64, 0.64, 
    0.64, 0.61, 0.68, 0.66, 0.71, 0.92, 0.99, 1.1, 0.95, 1.09, 
    1, 0.98, 1.03, 1, 0.86, 0.91, 0.81, 0.8, 0.79, 0.78, 0.72, 
    0.78, 0.8, 0.73, 0.74, 0.76, 0.72, 0.72, 0.69, 0.65, 0.68, 
    0.67, 0.68, 0.72, 0.64, 0.66, 0.65, 0.65, 0.75, 0.74, 0.76, 
    0.75, 0.73, 0.72, 0.72, 0.68, 0.71, 0.75, 0.75, 0.61, 0.61, 
    0.59, 0.58, 0.57, 0.59, 0.58, 0.61, 0.57, 0.61, 0.61, 0.89, 
    0.84, 0.86, 0.91, 0.81, 0.89, 0.89, 0.83, 0.86, 0.88, 0.86, 
    0.9, 0.94, 0.94, 0.9, 0.93, 0.93, 0.91, 0.86, 0.83, 0.8, 
    0.9, 0.74, 0.78, 0.8, 0.78, 0.77, 0.84, 0.8, 0.78, 0.75, 
    0.78, 0.78, 0.77, 0.82, 0.73, 0.78, 0.77, 0.83, 0.75, 0.77, 
    0.76, 0.72, 0.74, 0.78, 0.8, 0.74, 0.73, 0.77, 0.81, 0.76, 
    0.78, 0.72, 0.73, 0.76, 0.81, 0.82, 0.78, 0.79, 0.77, 0.83, 
    0.75, 0.83, 0.76, 0.72, 0.74, 0.71, 0.72, 0.73, 0.74, 0.71, 
    0.73, 0.75, 0.77, 0.76, 0.78, 0.74, 0.82, 0.83, 0.78, 0.84, 
    0.83, 0.83, 0.77, 0.78, 0.76, 0.9, 0.85, 0.71, 0.84, 0.73, 
    0.78, 0.73, 0.81, 0.74, 0.77, 0.81, 0.88, 0.81), fstim = c(0L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 0L, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 9L, 10L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
    10L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 0L, 1L, 
    2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 0L, 1L, 2L, 3L, 4L, 
    5L, 6L, 7L,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L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L), fpro = c(1, 0.97, 0.95, 0.88, 
    0.58, 0.47, 0.35, 0.13, 0.13, 0.01, 0.05, 0.81, 0.78, 0.83, 
    0.71, 0.61, 0.41, 0.45, 0.22, 0.22, 0.16, 0.18, 0.9, 0.9, 
    0.84, 0.83, 0.78, 0.69, 0.49, 0.47, 0.38, 0.18, 0.13, 0.96, 
    0.97, 0.95, 0.94, 0.82, 0.64, 0.34, 0.05, 0.08, 0.01, 0.03, 
    0.96, 0.94, 0.91, 0.88, 0.55, 0.55, 0.38, 0.18, 0.05, 0.04, 
    0.05, 0.92, 0.92, 0.93, 0.96, 0.86, 0.71, 0.58, 0.26, 0.13, 
    0.13, 0.12, 0.99, 1, 0.96, 0.9, 0.81, 0.58, 0.26, 0.09, 0.5, 
    0.02, 0.05, 0.92, 0.97, 0.94, 0.94, 0.75, 0.69, 0.42, 0.14, 
    0.01, 0.08, 0.05, 0.96, 0.95, 0.96, 0.91, 0.74, 0.64, 0.45, 
    0.27, 0.18, 0.08, 0.12, 0.99, 0.97, 0.95, 0.79, 0.63, 0.52, 
    0.38, 0.09, 0.03, 0.03, 0.01, 1, 0.97, 0.96, 0.93, 0.66, 
    0.51, 0.29, 0.1, 0.05, 0.01, 0.03, 0.98, 0.94, 0.83, 0.55, 
    0.27, 0.12, 0.1, 0.01, 0, 0.05, 0.01, 0.97, 0.98, 0.91, 0.79, 
    0.6, 0.32, 0.14, 0.06, 0.06, 0.09, 0.02, 0.94, 0.95, 0.87, 
    0.84, 0.64, 0.29, 0.16, 0.1, 0.06, 0.01, 0.08, 0.98, 0.92, 
    0.91, 0.79, 0.66, 0.42, 0.31, 0.13, 0.12, 0.09, 0.06, 1, 
    0.97, 1, 0.95, 0.87, 0.71, 0.4, 0.3, 0.1, 0.03, 0.06, 0.97, 
    0.98, 0.97, 0.85, 0.62, 0.41, 0.39, 0.17, 0.13, 0.08, 0.05, 
    0.95, 0.97, 0.97, 0.88, 0.66, 0.35, 0.22, 0.14, 0.07, 0.09, 
    0.01, 0.94, 0.92, 0.87, 0.79, 0.65, 0.56, 0.47, 0.3, 0.19, 
    0.1, 0.03, 0.96, 0.95, 0.92, 0.94, 0.65, 0.49, 0.34, 0.19, 
    0.08, 0.07, 0.05, 0.56, 0.52, 0.61, 0.54, 0.53, 0.58, 0.49, 
    0.43, 0.45, 0.35, 0.18, 0.75, 0.71, 0.78, 0.76, 0.7, 0.65, 
    0.65, 0.6, 0.49, 0.4, 0.29, 0.54, 0.55, 0.51, 0.49, 0.53, 
    0.42, 0.6, 0.38, 0.42, 0.34, 0.22, 0.89, 0.66, 0.69, 0.52, 
    0.53, 0.47, 0.62, 0.43, 0.4, 0.37, 0.32, 0.49, 0.48, 0.38, 
    0.39, 0.34, 0.39, 0.38, 0.26, 0.19, 0.26, 0.23, 0.49, 0.51, 
    0.44, 0.39, 0.34, 0.41, 0.38, 0.28, 0.21, 0.26, 0.23, 0.53, 
    0.49, 0.48, 0.39, 0.34, 0.39, 0.38, 0.26, 0.19, 0.26, 0.23, 
    0.49, 0.48, 0.53, 0.49, 0.44, 0.39, 0.38, 0.27, 0.21, 0.2, 
    0.18, 0.58, 0.57, 0.58, 0.59, 0.53, 0.46, 0.38, 0.27, 0.19, 
    0.15, 0.23, 0.49, 0.48, 0.53, 0.53, 0.44, 0.51, 0.44, 0.46, 
    0.39, 0.36, 0.32), frt = c(1.13, 1.06, 1.09, 1.08, 1.09, 
    1.17, 1.12, 1.15, 1.08, 1.09, 1.1, 0.86, 0.89, 0.81, 0.88, 
    0.92, 0.89, 0.86, 0.84, 0.86, 0.93, 0.92, 1.17, 1.2, 1.28, 
    1.24, 1.31, 1.34, 1.27, 1.29, 1.18, 1.15, 1.16, 0.86, 0.83, 
    0.87, 0.94, 0.91, 0.92, 0.92, 1.05, 0.94, 0.92, 0.89, 0.88, 
    0.87, 0.89, 0.89, 0.92, 0.9, 0.89, 0.91, 0.85, 0.92, 0.9, 
    0.88, 0.86, 0.89, 0.84, 0.9, 0.88, 0.95, 0.9, 0.86, 0.87, 
    0.83, 0.7, 0.72, 0.76, 0.78, 0.82, 0.75, 0.77, 0.82, 0.76, 
    0.74, 0.75, 0.72, 0.7, 0.69, 0.71, 0.69, 0.73, 0.74, 0.71, 
    0.71, 0.7, 0.74, 0.94, 0.97, 0.93, 1.02, 0.95, 1.08, 1.02, 
    1.03, 1.09, 0.92, 1.01, 0.94, 0.95, 0.95, 0.96, 0.97, 1, 
    0.98, 0.99, 0.95, 0.94, 0.94, 0.63, 0.61, 0.66, 0.67, 0.66, 
    0.7, 0.71, 0.67, 0.66, 0.66, 0.61, 0.81, 0.81, 0.8, 0.89, 
    0.81, 0.81, 0.79, 0.74, 0.79, 0.79, 0.79, 0.76, 0.74, 0.79, 
    0.81, 0.9, 0.95, 0.88, 0.77, 0.71, 0.76, 0.75, 0.65, 0.74, 
    0.71, 0.68, 0.67, 0.76, 0.72, 0.75, 0.73, 0.72, 0.76, 0.72, 
    0.67, 0.69, 0.7, 0.69, 0.73, 0.73, 0.73, 0.68, 0.71, 0.72, 
    0.62, 0.6, 0.6, 0.64, 0.66, 0.63, 0.68, 0.74, 0.7, 0.68, 
    0.64, 0.77, 0.72, 0.74, 0.76, 1.05, 1.11, 1.07, 0.81, 0.83, 
    0.81, 0.82, 0.67, 0.69, 0.73, 0.79, 0.8, 0.86, 0.84, 0.75, 
    0.75, 0.75, 0.73, 0.69, 0.69, 0.68, 0.67, 0.63, 0.66, 0.67, 
    0.67, 0.68, 0.69, 0.68, 0.65, 0.68, 0.69, 0.74, 0.78, 0.83, 
    0.79, 0.74, 0.72, 0.74, 0.75, 0.64, 0.64, 0.67, 0.67, 0.7, 
    0.76, 0.75, 0.71, 0.71, 0.65, 0.65, 0.82, 0.85, 0.84, 0.87, 
    0.85, 0.85, 0.85, 0.9, 0.97, 0.87, 0.85, 0.87, 0.87, 0.83, 
    0.86, 0.9, 0.93, 0.97, 0.92, 0.91, 0.92, 0.86, 0.81, 0.73, 
    0.78, 0.77, 0.76, 0.8, 0.82, 0.81, 0.81, 0.74, 0.77, 0.85, 
    0.82, 0.81, 0.77, 0.76, 0.71, 0.83, 0.69, 0.7, 0.78, 0.73, 
    0.86, 0.81, 0.82, 0.78, 0.75, 0.72, 0.82, 0.69, 0.7, 0.78, 
    0.74, 0.8, 0.81, 0.77, 0.76, 0.76, 0.71, 0.83, 0.73, 0.7, 
    0.78, 0.61, 0.7, 0.72, 0.81, 0.75, 0.76, 0.71, 0.73, 0.79, 
    0.73, 0.78, 0.73, 0.75, 0.77, 0.81, 0.77, 0.75, 0.72, 0.83, 
    0.76, 0.81, 0.78, 0.74, 0.71, 0.72, 0.71, 0.77, 0.76, 0.72, 
    0.83, 0.78, 0.8, 0.78, 0.74)), .Names = c("participant", 
"group", "dstim", "dpro", "drt", "fstim", "fpro", "frt"), row.names = c(NA, 
-330L), class = "data.frame")
  • Now we need to set up the groups and add standard deviation
ident$group <- factor(ident$group, levels = c("EL", "NE", "LL"))
df<-with(ident, aggregate(fpro, list(group=group, fstim=fstim), mean))
df$se<-with(ident, aggregate(fpro, list(group=group, fstim=fstim), function(x) sd(x)/sqrt(10)))[,3]
  • Here is the code for the plot
gp<-ggplot(df, aes(x=fstim, y=x, colour=group, ymin=x-se, ymax=x+se))
gp + geom_line(aes(linetype=group), size = .7) + 
    geom_point(aes(shape=group)) + 
    geom_ribbon(alpha = 0.05, linetype=0) + 
    #geom_errorbar(aes(ymax=x+se, ymin=x-se)) +
    ylim(0, 1) + 
    scale_x_continuous(breaks=seq(0, 10, by=1)) +
    labs(list(title = "", 
              x = "Stimuli", y = "% Sheep")) +
    theme_bw() +
    theme(legend.position = c(0.909, 0.90)) +
    theme(plot.title = element_text(size = rel(1))) +
    theme(legend.text = element_text(size = 12)) +
    theme(legend.title = element_text(size = 16)) +
    theme(legend.background = element_rect(colour = 'grey90', 
                fill = 'grey97', size = .65, linetype='solid')) +
    scale_linetype_discrete("Group") +
    scale_shape_discrete("Group") +
    scale_colour_discrete("Group") +
    theme(axis.title.x = element_text(size = 16)) +
    theme(axis.title.y = element_text(size = 16)) +
    theme(axis.text.y = element_text(size=16)) +
    theme(axis.text.x = element_text(size=16))

comments powered by Disqus