Tutorial: Turing Table into Line Graphs (ggplot2)

Step 1: Obtain the data from table and turn into R readable tabular format

a. observe the data in the table and decide how many rows and columns you're going to have for the tabular

b. Type in the data from the table to Microsoft Excel sheet (it's more convenient). The first row should be the field names. You may need to add the first row or certain field names in first row manually.

c.save the excel file into .csv file format

Step 2: Import the data to R

Use the code below to import the data, name your own data

table2 = read.csv("~/Documents/M.A. @ UMN/spring 2013/analysis 2/assignment 1/table2.csv")

or you could click the “Import Dataset” button and choose your text file or any file from website.

Step 3: Install ggplot 2 package and run the library

use the code below to install and run the ggplot2 library

install.packages("ggplot2")
## Installing package(s) into
## '/Volumes/RStudio-0.97.312/RStudio.app/Contents/Resources/R/library' (as
## 'lib' is unspecified)
## Warning: 'lib =
## "/Volumes/RStudio-0.97.312/RStudio.app/Contents/Resources/R/library"' is
## not writable
## Error: unable to install packages
library(ggplot2)

or you could click on “packages” >> “install Packages”>> enter “ggplot2” in the text box>> click “install”

Step 4: plot-drawing

a. draw the basic geometry shapes in plot A (y value = “Mean”). The graph I'm going to draw is a line graph consisting of two lines, representing two sections: “Near future ” and “Distant future.”

ggplot(data = table2, aes(x = TraitDimension, y = M, group = Section)) + geom_line() + 
    geom_point()

plot of chunk unnamed-chunk-3

b. add colors to the lines and points to distinguish the two sections

ggplot(data = table2, aes(x = TraitDimension, y = M, group = Section, color = Section)) + 
    geom_line() + geom_point()

plot of chunk unnamed-chunk-4

c. add shapes and fill the color to the points

ggplot(data = table2, aes(x = TraitDimension, y = M, group = Section, color = Section, 
    shape = Section)) + geom_line() + geom_point(fill = "white") + scale_shape_manual(values = c(22, 
    21))

plot of chunk unnamed-chunk-5

d. add types to the lines to fartherly distinguish the two comparing sections

ggplot(data = table2, aes(x = TraitDimension, y = M, group = Section, color = Section, 
    shape = Section)) + geom_line(aes(linetype = Section)) + geom_point(fill = "white") + 
    scale_shape_manual(values = c(22, 21))

plot of chunk unnamed-chunk-6

e. add sizes to the lines and points

ggplot(data = table2, aes(x = TraitDimension, y = M, group = Section, color = Section, 
    shape = Section)) + geom_line(size = 1.5) + geom_point(size = 3, fill = "white") + 
    scale_shape_manual(values = c(22, 21))

plot of chunk unnamed-chunk-7

f. add x-axis and y-axis

ggplot(data = table2, aes(x = TraitDimension, y = M, group = Section, color = Section, 
    shape = Section)) + geom_line(aes(linetype = Section), size = 1.5) + geom_point(size = 3, 
    fill = "white") + scale_shape_manual(values = c(22, 21)) + xlab("Trait Dimension") + 
    ylab("Mean")

plot of chunk unnamed-chunk-8

g. add title to the plot

ggplot(data = table2, aes(x = TraitDimension, y = M, group = Section, color = Section, 
    shape = Section)) + geom_line(aes(linetype = Section), size = 1.5) + geom_point(size = 3, 
    fill = "white") + scale_shape_manual(values = c(22, 21)) + xlab("Trait Dimension") + 
    ylab("Mean") + ggtitle("Cross-Situational Variance Scores as a Function of Temporal Distance: study 5-Part 1")

plot of chunk unnamed-chunk-9

h. follow the same steps when doing plot B(y value = “Standard Deviation”)

ggplot(data = table2, aes(x = TraitDimension, y = SD, group = Section, color = Section, 
    shape = Section)) + geom_line(aes(linetype = Section), size = 1.5) + geom_point(size = 3, 
    fill = "white") + scale_shape_manual(values = c(22, 21)) + xlab("Trait Dimension") + 
    ylab("Standard Deviation") + ggtitle("Cross-Situational Variance Scores as a Function of Temporal Distance: study 5-Part 2")

plot of chunk unnamed-chunk-10

Step 5: Final plots A & B

ggplot(data = table2, aes(x = TraitDimension, y = M, group = Section, color = Section, 
    shape = Section)) + geom_line(aes(linetype = Section), size = 1.5) + geom_point(size = 3, 
    fill = "white") + scale_shape_manual(values = c(22, 21)) + xlab("Trait Dimension") + 
    ylab("Mean") + ggtitle("Cross-Situational Variance Scores as a Function of Temporal Distance: study 5-Part 1")

plot of chunk unnamed-chunk-11

ggplot(data = table2, aes(x = TraitDimension, y = SD, group = Section, color = Section, 
    shape = Section)) + geom_line(aes(linetype = Section), size = 1.5) + geom_point(size = 3, 
    fill = "white") + scale_shape_manual(values = c(22, 21)) + xlab("Trait Dimension") + 
    ylab("Standard Deviation") + ggtitle("Cross-Situational Variance Scores as a Function of Temporal Distance: study 5-Part 2")

plot of chunk unnamed-chunk-11