# Holcombe:Plotting

Alex Holcombe
• Ryo Nakayama

### Other

Good advice on graph choice, from human perceptual standpoint by Frank Harrell

## ggplot2

In the lab we usually use the R package, ggplot2, for graphs.

For custom colour schemes, the Chart of R Colors is a helpful resource. The table with named colours is most useful.

ggplot2 tips:

#where 'g' is your ggplot object
str(g)  #gives you everything!
summary(g) #gives a summary
options(g) #lists the options
last_plot() #refreshes the last plot and returns the struct

p=p+scale_y_continuous(breaks=c(0,0.5,1),minor_breaks=c(.25,.75)) #changing axis breaks
p=p+opts(title="one-behind errors")
p=p+xlab('relative phase')

opts(panel.grid.minor = theme_blank())+

last_plot() + opts(panel.grid.minor = theme_line(colour = "black") )

facet_grid(.~subject)  # rows~columns

g<-g+stat_summary(fun.y=mean,geom="point",position="jitter") #getting jitter to work when you're collapsing across other variables with stat_summary

g=g+ stat_smooth(method="glm",family="binomial",fullrange=TRUE) #adds logistic smoother to plot
#it's impossible to extract the function fits however because they're fit on the fly
#how can I fit an arbitrary function, like a psychometric function, e.g. cumulative gaussian with chance rate and lapse rate? Logistic is restricted to 0->1



order used for scale mapping (color, etc.) is perhaps the order of the levels property of the vector. This gives bizarre results because R's sort(unique(x)) default does not alphabetize strings. Getting things to appear in a graph or something the way you want may require reordering the 'levels' of a factor, like this to put DL as the last subject to be graphed:

pvalsCIs$subject<-factor(pvalsCIs$subject,levels=c("AH","SM","SYL","WYC","DL")) #levels(pvalsCIs\$subject)


### polishing plots

See Chapter 8 of the ggplot book

p+ theme_bw() #changes all options to those of theme_bw
theme_bw() #see associated list of options
p + opts( axis.text.x= theme_text(size = base_size * 0.8 , lineheight = 0.9, colour = "grey50", hjust = 1, angle = 90) )
p+opts(legend.key=theme_rect(fill=NULL,colour="white"))

#remove shading of facet_grid labels and lines around them, for better post-production editing
p+ opts(strip.background = theme_rect(colour = 'white', fill=NULL))

#turn off grid
p+ opts(panel.grid.major=theme_blank(), panel.grid.minor=theme_blank())

#change font. You can set the font of the labels produced by geom_text with grid.gedit:
grid.gedit("GRID.text",gp=gpar(fontfamily="mono"))
#Call this after you have produced your original plot.

p+opts(axis.title.x=theme_text(vjust=1)) #for some reason this changes the size of the axis label

p+opts(axis.text.x=theme_text(hjust=0.1)) #for some reason this seems to displace horizontally the ticks labels


ok