superscript

for example in a plot title:


ylab = expression(paste("Lignin content (mg ", g^-1, ")", sep = ""))
()
Make a barplot with errorbars
Now this is a tricky one: I wrote a script to plot a barplot with errorbars. I used the following script:

#barplot where x is the independent on the x-axis, y is the 
#dependent on the y-axis and z is the independent given by 
#different colored bars
anova.plot<-function(x, y, z, ylab="y", xlab="x", 
ylim=c(0, max(xx)+max(yy)), length=0.05){

#height of the bars
xx<-tapply(y,list(z,x),mean)

#standard deviation
yy<-tapply(y,list(z,x),sd)

#number of replicates
zz<-tapply(y,list(z,x),length)

#standard error
er<-yy/sqrt(zz)

#number of colors for bars
w<-length(levels(z))

#simple barplot without the errorbars
barx<-barplot(xx, col=c(1:w), beside=T, ylab=ylab, xlab=xlab, 
ylim=ylim,xpd=FALSE)

#box around the plot
box()

#error bars
arrows(barx,xx+er, barx, xx, angle=90, code=1, length=length)

#legend (after making the plot, indicate where the legend has 
#to come with the mouse)
legend(locator(1),c(levels(z)),fill=c(1:w),bty="n",cex=0.8)
}


If you have set your palette to:
palette(c("grey25","grey50","grey75","white"))
you get a plot like the one above if you use:
anova.plot(x,y,z)

Make a barplot with errorbars

Now this is a tricky one: I wrote a script to plot a barplot with errorbars. I used the following script:


#barplot where x is the independent on the x-axis, y is the 
#dependent on the y-axis and z is the independent given by 
#different colored bars
anova.plot<-function(x, y, z, ylab="y", xlab="x", 
ylim=c(0, max(xx)+max(yy)), length=0.05){

#height of the bars
xx<-tapply(y,list(z,x),mean)

#standard deviation
yy<-tapply(y,list(z,x),sd)

#number of replicates
zz<-tapply(y,list(z,x),length)

#standard error
er<-yy/sqrt(zz)

#number of colors for bars
w<-length(levels(z))

#simple barplot without the errorbars
barx<-barplot(xx, col=c(1:w), beside=T, ylab=ylab, xlab=xlab, 
ylim=ylim,xpd=FALSE)

#box around the plot
box()

#error bars
arrows(barx,xx+er, barx, xx, angle=90, code=1, length=length)

#legend (after making the plot, indicate where the legend has 
#to come with the mouse)
legend(locator(1),c(levels(z)),fill=c(1:w),bty="n",cex=0.8)
}

If you have set your palette to:

palette(c("grey25","grey50","grey75","white"))

you get a plot like the one above if you use:

anova.plot(x,y,z)
()

Make a subset

To make subsets from a large dataset you can use the command subset(). For example, if you have some outliers in your dependent, in my case some negative values where there can be only positive values, you can use:

subdata<-subset(data, dependent>0)

Now subdata is the dataset without the outliers. If you don’t want a particular treatment into your analysis you can use:

subdata<-subset(data, factor=="C")

Now subdata is the dataset of only the factor C.

()

Export Anova table

I had some trouble transporting tables from R to Word, because the tabs that R gives you change into spaces in Word, resulting in a messy table. Here is a solution for an Anova table using the command capture.output():

#make the anova table
anova<-aov(Lignin~treatment*temp, data=lignout)
summary(anova)

#export it to wordfile
capture.output(summary(anova),file="test.doc")

R makes a Word file called test.doc on your computer. In my case R puts this document in my default documents folder. Word might ask how to import the data. This also works when you export the table as a .xls file. Excel understands the tabs and puts the table in the right cells.

()

Anova

Doing an Anova in R is easy. If you want a simple one-way Anova where weight is the dependent and treatment is the fixed factor you type:

data<-aov(weight~treatment)

summary(data)

Now you have an Anova table. For a factorial Anova just type:

data<-aov(weight~treatment*temperature)

summary(data)

Now you have the interaction effects of treatment and temperature of weight. You can also plot several Anova outcomes, like a Q-Q plot with:

plot(data)

()
Make a barplot
To make a barplot is simple. To make a publishable barplot is tricky in R. The above shown figure is the closest to a publishable barplot as I can get at this moment. Error bars in a barplot is not simply included in R, because error bars do not belong in a barplot that depicts an Anova for example. Now, here is the script for this plot, I used the palette of colors I described before:

barplot(tapply(Lignin,list(treatment,temp),mean), col=c(1,2,3), 
beside=T, ylab="Lignin content (mg/g DW)", 
xlab="Plot Temperature (°C)", ylim=c(0,160))

legend(locator(1), c("C","N","NP"), fill=c(1,2,3), bty="n", cex=0.8)
You can use ?barplot and ?legend to check what all the different commands are for. The most interesting is locator(1) I think. This gives you the opportunity to choose the location of the legend in your figure with a click of the mouse.

Make a barplot

To make a barplot is simple. To make a publishable barplot is tricky in R. The above shown figure is the closest to a publishable barplot as I can get at this moment. Error bars in a barplot is not simply included in R, because error bars do not belong in a barplot that depicts an Anova for example. Now, here is the script for this plot, I used the palette of colors I described before:


barplot(tapply(Lignin,list(treatment,temp),mean), col=c(1,2,3), 
beside=T, ylab="Lignin content (mg/g DW)", 
xlab="Plot Temperature (°C)", ylim=c(0,160))

legend(locator(1), c("C","N","NP"), fill=c(1,2,3), bty="n", cex=0.8)

You can use ?barplot and ?legend to check what all the different commands are for. The most interesting is locator(1) I think. This gives you the opportunity to choose the location of the legend in your figure with a click of the mouse.

()

Set color palette

The default colors of R when you plot a barplot are hideous. I like to have just black and white and grayscales in my plots. To set a customize palette of colors you can use the command palette(). If you type in colors(), R gives you all the names of colors you can pick. In my case I want a palette of three colors, black, gray and white:

palette(c("black","gray","white"))

I can now refer to the colors with numbers. In my barplot command I can use col=c(1,2,3) if I have three separate bars. If I have two bars I can use col=c(1,3) when I only want black and white.

()

Rename column titles

You often come across the following warning in R:

The following object(s) are masked from data

This is because two columns in different datasets have the same name. It is very easy to rename the column titles of a dataset. I have attached the names of a dataset with attach(). When I give the command names() I’ll see the title names. Say I want to rename the first column title I only have to typ:

names(data)[1]<-"changed name"

If I want to change the second column title I have to change the 1 into a 2. If I want to change all the names I can give the following command:

names(data)<-c("changed name 1","changed name 2","etc.")

()