Getting figures publication ready

Part I: R with ggplot2
The ggplot2 packages for R has some fantastic features for very powerful, flexible, and aesthetic data visualisation (If you are not familiar with the packages, you can have a look at some of the capabilities here: [http://docs.ggplot2.org/current/]). It is also relatively easy to export figures in a way that matches any journals figure specifications.

I will use the sample dataset ‘iris’ that is included with R for the following demonstration. This dataset contains data on petal length of three iris species among other measures.


# Loading the data
data(iris)

# Producing a basic plot
ggplot(data=iris,aes(x=factor(Species), y=Petal.Length)) +
geom_boxplot() +
xlab('Species') +
ylab('petal length [cm]')

Example_plot1

For publication, we would probably like to make the main features of the plot a bit bolder, control the font size of the axis labels, and use a white background:


figure <- ggplot(data=iris,aes(x=factor(Species), y=Petal.Length)) +
geom_boxplot(width=0.5,lwd=1,fill='grey') +
xlab('Species') +
ylab('petal length [cm]') +
theme_bw() +
theme(axis.text=element_text(size=12),
axis.title=element_text(size=13))

Example_plot2

We might also want to include annotations that indicate results of statistical analyses. Here is a one-way ANOVA to compare petal length between species followed by post-hoc t-tests to determine differences between species pairs:


# one-way ANOVA:
summary(aov(data=iris,Petal.Length ~ Species))

# t-test single contrasts:
t.test(iris$Petal.Length[iris$Species=='setosa'],iris$Petal.Length[iris$Species=='versicolor'],paired=FALSE)
t.test(iris$Petal.Length[iris$Species=='setosa'],iris$Petal.Length[iris$Species=='virginica'],paired=FALSE)
t.test(iris$Petal.Length[iris$Species=='versicolor'],iris$Petal.Length[iris$Species=='virginica'],paired=FALSE)

This analysis indicates that there are significant differences between all species in petal length. Next, we will add information about the group differences to the boxplot for the convenience of the reader:


figure +
 geom_segment(aes(x=1, y=7, xend=2, yend=7), size=0.1) +
 geom_segment(aes(x=2, y=7.2, xend=3, yend=7.2), size=0.1) +
 geom_segment(aes(x=1, y=7.4, xend=3, yend=7.4), size=0.1) +
 annotate("text",x=1.5, y=7,label="*",size=8) +
 annotate("text",x=2.5, y=7.2,label="*",size=8) +
 annotate("text",x=2, y=7.5, label="*",size=8)

Example_plot3

The final step is to export the figure with properties that match the specifications of the publisher. As an example, I will export a figure with 4cm height and 3cm width at 300 dpi resolution in PNG format:


ggsave(figure,file=‘/Users/joebathelt/Example.png’,   width=3,height=4,dpi=300,limitsize=TRUE)

Example_plot4

Advertisements

Leave a Response

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s