Skip to content

Instantly share code, notes, and snippets.

@schluppeck
Last active March 13, 2017 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schluppeck/9a54b9b7a37793d8959779629b4cd2fc to your computer and use it in GitHub Desktop.
Save schluppeck/9a54b9b7a37793d8959779629b4cd2fc to your computer and use it in GitHub Desktop.
grammar of graphics - ideas and how to in R / Matlab
# re-create plot for vision group talk 20170309
#
# ds
library(ggplot2)
library(dplyr)
library(tidyr)
# if you get an error about unknown packages, you will probably have to run the following first:
# install.packages(c("ggplot2","tidyr","dplyr"))
# random seed - probably not strictly necessary
set.seed(12345)
# data for figure 3b from Zamboni et al (2016)
d = read.csv('xy-dip-data+.csv')
d$subject = factor(d$subject) # make it categorical
# ceate ggplot object with the appropriate data -> aes mappings
q <- ggplot(data = d,
aes(x = absent, y = present) )
# decide on the ``geom``- this will also display it
q + geom_point()
# actually, we want the full mapping
p <- ggplot(data = d,
aes(x = absent, y = present, size = coherence, color = subject) )
p + geom_point()
## now deal with ``scale`` + the annotations.
finalPlot <- p + geom_point() +
scale_size( breaks = c(4,7,13,25) ) + # could also do breaks = unique(d$coherence)
scale_x_continuous( limits = c(0, 0.1) ) +
scale_y_continuous( limits = c(0, 0.1) ) +
coord_fixed() +
geom_abline(slope = 1, intercept = 0, size = 1, color="Gray")
# and display
finalPlot + theme_bw()
# Themes!
# can easily change the look of your plots (this is also customisable)
p1 <- finalPlot + theme_classic()
p2 <- finalPlot + theme_minimal()
p3 <- finalPlot + theme_dark()
p4 <- finalPlot + theme_light()
source("http://peterhaschke.com/Code/multiplot.R")
multiplot(p1, p2, p3, p4, cols = 2)
# Facets!
finalPlot +
facet_wrap(~subject) +
theme_minimal() +
labs(title = "Data for different subjects")
# Facets!
finalPlot +
facet_wrap(~coherence) +
theme_minimal() +
labs(title = "Data by coherence")
# ... but how if we want to do this with bar plots?
# data need to be in tidy format...
e = gather(d, condition, statistic, -X, -subject, -coherence)
head(e)
r <- ggplot(data = e,
aes(x = subject, y = statistic,
fill = condition, color = condition) )
r + geom_bar(stat= "identity", position="dodge")
%% introduction to gramm
%
% ds 20170309
%
% the following is adapted from the website for GRAMM
% https://github.com/piermorel/gramm
clear all
close all
load carbig.mat %Load example dataset about cars
origin_region=num2cell(org,2); %Convert origin data to a cellstr
%% GRAMM uses object-oriented approach
% CREATE a gramm object that contains the mapping from data -> AES
figure
g=gramm('x',Model_Year,'y',MPG,'color',Cylinders, 'subset',Cylinders~=3 & Cylinders~=5)
% Plot raw data as points
g.geom_point()
% Set appropriate names for legends
g.set_names('column','Origin','x','Year','y','Fuel economy (MPG)','color','# Cylinders')
%Set figure title
g.set_title('Fuel economy of new cars between 1970 and 1982')
% Do the actual drawing
g.draw()
%% try again, but add a facet grid and linear fit
figure
h=gramm('x',Model_Year,'y',MPG,'color',Cylinders, 'subset',Cylinders~=3 & Cylinders~=5)
h.geom_point()
% Plot linear fits of the data with associated confidence intervals
h.stat_glm()
% Subdivide the data in subplots horizontally by region of origin
h.facet_grid([],origin_region)
% and draw this one
h.draw()
subject coherence absent present
1 1 4 0.035996 0.087079
2 2 7 0.026042 0.056272
3 3 13 0.028604 0.047791
4 4 25 0.029221 0.063149
5 5 4 0.025157 0.096832
6 1 7 0.041558 0.09116
7 2 13 0.029412 0.043644
8 3 25 0.045673 0.063584
9 4 4 0.03071 0.081536
10 5 7 0.029693 0.09596
11 1 13 0.029868 0.088212
12 2 25 0.020048 0.050617
13 3 4 0.033784 0.055389
14 4 7 0.031633 0.073497
15 5 13 0.035731 0.091174
16 1 25 0.056707 0.068027
17 2 4 0.033773 0.051282
18 3 7 0.038994 0.035436
19 4 13 0.024133 0.064919
20 5 25 0.026471 0.09769
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment