Pedro Alcocer

CV

Personal

Code


Plotting MEG data with R and ggplot2

Friday, February 26, 2010


Here's a bit of R code for plotting the processed MEG data that Cephalo outputs. It uses the ggplot2 plotting package for easy, good-looking plots.

library(ggplot2)

# Download some data.
meg.data = read.csv("http://pealco.net/downloads/ws2010_meg.csv")

# Create a root mean square function.
rms = function(x) { sqrt(mean(x**2)) }

# Set up x and y aesthetic mappings.
meg.plot = ggplot(meg.data, aes(x = sample, y = amplitude))

# Apply a line geom, faceting, and a stat layer.
meg.plot + geom_line(aes(group=channel), color=alpha("black", 1/4)) +
           facet_grid(factor1 ~ factor0) +
           stat_summary(fun.y = "rms", color = "red", geom="line")

The result: