KRIGOLSON TEACHING
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245 >
      • LABORATORY
      • PRACTICUM
    • EPHE 357
  • STATISTICS
    • LECTURE >
      • INTRODUCTION TO R
      • DESCRIPTIVE STATISTICS
      • VISUALIZING DATA
      • Correlation and Regression
      • MULTIPLE REGRESSION
      • LOGIC OF NHST
      • T TESTS
      • ANOVA
      • POST HOC ANALYSIS
      • NON PARAMETRIC STATISTICS
      • FACTORIAL ANOVA
      • Repeated Measures ANOVA
      • Mixed ANOVA
      • MULTIVARIATE ANOVA
      • THE NEW STATISTICS
      • Bayesian Methods
    • ASSIGNMENTS >
      • Introduction to R >
        • INTRODUCTION TO R
        • LOADING DATA
        • DATA TABLES
      • Descriptive Statistics >
        • Mean, Median, and Mode
        • VARIANCE
        • CONFIDENCE INTERVALS
        • SHORTCUTS
      • Visualizing Data >
        • PLOTTING BASICS
        • BAR GRAPHS
        • BOXPLOTS
        • HISTOGRAMS
        • USING GGPLOT I
        • USING GGPLOT II
        • USING GGPLOT III
      • Correlation and Regression >
        • CORRELATION
        • REGRESSION
      • MULTIPLE REGRESSION >
        • MULTIPLE REGRESSION
      • Logic of NHST >
        • Sample Size and Variance
        • DISTRIBUTIONS
        • TESTING DISTRIBUTIONS
      • T-Tests >
        • Single Sample TTests
        • Paired Sample TTests
        • Independent Sample TTests
      • ANOVA >
        • ANOVA ASSUMPTIONS
        • ANOVA
      • POST HOC ANALYSIS >
        • POSTHOC ANALYSIS
      • NON PARAMETRIC STATISTICS >
        • WILCOXON TEST
        • WILCOXON SIGNED TEST
        • MULTIPLE GROUPS
      • FACTORIAL ANOVA
      • REPEATED MEASURES ANOVA >
        • RM ANOVA
        • TREND ANALYSIS
      • MIXED ANOVA
      • MULTIVARIATE ANOVA
      • THE NEW STATISTICS
      • BAYESIAN TTESTS
    • RESOURCES
    • R TIPS
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops

3A. PLOTTING BASICS



In this lesson we will review the basics of plotting data in R. Some of this is covered in other lessons and some of it is not. For now, load the data file HERE into a table called "data".

Next, name the three columns "subject", "condition", and "rt". You will note there are three groups of 50 participants in this data set.

Let's generate a basic scatter plot: plot(data$rt)
You will note this command generates a scatter plot of the values in data$rt with subject as the x axis value and rt as the y axis value.

If you wanted to represent this data as a bar plot you would simply use: barplot(data$rt)

For now, let's return to a basic scatter plot: plot(data$rt)

If you wanted to visualize the group differences, you could use a plot like this: plot(data$rt~data$condition)
This essentially tells R that you want a scatter plot of data$rt but instead of data$subject on the x axis you would like to use data$condition. Try plot(data$rt~data$subject) as a proof of concept. The default in R for the plot command is to assume the x axis reflects each data point if nothing else is specified. Note, this will not work for barplots - you need to generate the means in advance. See below.

Fitting a regression line to a scatter plot in R is easy. Try: abline(lm(data$rt~data$subject))
This command adds a regression line (a linear model: lm) for data$rt as a function of data$subject.

You could simply construct the regression line (linear model) for the condition effect by stating:
plot(data$rt~data$condition)
abline(lm(data$rt~data$condition))


You can add other information to your plot. For example, if you want to label the plot axes in R you would do the following:
plot(data$rt~data$condition, main="Condition Effect", xlab="Condition", ylab="Reaction Time (ms)", ylim=c(0, 500))
Go HERE for a lot more information information about what you can add to plots.

We can make our graph even more sophisticated. Let's highlight the means for each group.
means = aggregate(rt~condition,data,mean)
This command gets the mean values for each condition in the table "data" and assigns them to the variable means. Type "means" to check.

Now try: points(means$condition,means$rt,pch=16,col='red')
You should see a red circle reflecting the mean in the middle of each scatter plot for condition.

A different way to visualize this data would be to plot each group separately using a line to reflect the mean as per a previous assignment. You would do this as follows:

par(mfrow=c(1,3))
plot(data$rt[data$condition==1])
abline(a = means$rt[1], b = 0, col = "red")

plot(data$rt[data$condition==2])

abline(a = means$rt[2], b = 0, col = "red")
plot(data$rt[data$condition==3])

abline(a = means$rt[3], b = 0, col = "red")

More details on plotting in R can be found HERE and HERE.

Assignment Question
1. Using a data set of your choice generate a plot similar to the one immediately above. Ensure you title the plot, label the axes, and make sure the x and y limits are the same on all of your sub plots.


  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245 >
      • LABORATORY
      • PRACTICUM
    • EPHE 357
  • STATISTICS
    • LECTURE >
      • INTRODUCTION TO R
      • DESCRIPTIVE STATISTICS
      • VISUALIZING DATA
      • Correlation and Regression
      • MULTIPLE REGRESSION
      • LOGIC OF NHST
      • T TESTS
      • ANOVA
      • POST HOC ANALYSIS
      • NON PARAMETRIC STATISTICS
      • FACTORIAL ANOVA
      • Repeated Measures ANOVA
      • Mixed ANOVA
      • MULTIVARIATE ANOVA
      • THE NEW STATISTICS
      • Bayesian Methods
    • ASSIGNMENTS >
      • Introduction to R >
        • INTRODUCTION TO R
        • LOADING DATA
        • DATA TABLES
      • Descriptive Statistics >
        • Mean, Median, and Mode
        • VARIANCE
        • CONFIDENCE INTERVALS
        • SHORTCUTS
      • Visualizing Data >
        • PLOTTING BASICS
        • BAR GRAPHS
        • BOXPLOTS
        • HISTOGRAMS
        • USING GGPLOT I
        • USING GGPLOT II
        • USING GGPLOT III
      • Correlation and Regression >
        • CORRELATION
        • REGRESSION
      • MULTIPLE REGRESSION >
        • MULTIPLE REGRESSION
      • Logic of NHST >
        • Sample Size and Variance
        • DISTRIBUTIONS
        • TESTING DISTRIBUTIONS
      • T-Tests >
        • Single Sample TTests
        • Paired Sample TTests
        • Independent Sample TTests
      • ANOVA >
        • ANOVA ASSUMPTIONS
        • ANOVA
      • POST HOC ANALYSIS >
        • POSTHOC ANALYSIS
      • NON PARAMETRIC STATISTICS >
        • WILCOXON TEST
        • WILCOXON SIGNED TEST
        • MULTIPLE GROUPS
      • FACTORIAL ANOVA
      • REPEATED MEASURES ANOVA >
        • RM ANOVA
        • TREND ANALYSIS
      • MIXED ANOVA
      • MULTIVARIATE ANOVA
      • THE NEW STATISTICS
      • BAYESIAN TTESTS
    • RESOURCES
    • R TIPS
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops