Try this:

# I created some example data
mydf <- data.frame(
        Species=sample(paste0("Species", 1:3), 50, TRUE), 
        d13C=15+rnorm(50), 
        d15N=15+rnorm(50), 
        Year=sample(2009:2012, 50, TRUE))
attach(mydf)
Nmean <- tapply(d15N, list(Year,Species), mean)
Cmean <- tapply(d13C, list(Year,Species), mean)
detach(mydf)

# create an empty plot first with the correct x and y limits on the axes
plot(1, 1, xlim=range(Cmean), ylim=range(Nmean), type="n", 
        xlab="Cmean", ylab="Nmean", 
        main="Color indicates year, symbol indicates species")
# then add text for species, coloring by year
# convert the matrices to vectors before plotting
# use the col() and row() functions to get column (species)
#       and row (year) indicators
text(as.vector(Cmean), as.vector(Nmean), 
        dimnames(Cmean)[[2]][col(Cmean)], 
        col=c("green", "black", "red", "blue")[row(Cmean)]) 

Jean


monaR <mfu...@uit.no> wrote on 09/03/2012 12:33:20 PM:
> 
> Hei,
> i am trying to plot the means of two variables (d13C and d15N), by 2
> grouping factors (Species and Year) that i obtained by the function 
tapply.
> I would like to plot with different colours according to the Year and 
show
> the "Species" as data labels.
> My data looks like this:
> 
> Species          d13C    d13N    Year
> "Species1"    14,4       11.5     2009
> "Species2"       ...            ...       ....
> 
> >Nmean<-tapply(d15N,list(Year,Species),mean)
> >Cmean<-tapply(d13C,list(Year,Species),mean) 
> 
> ##works fine, returns something like this
> 
>           Species1         Species2         Species3
> 2009 20.3                  13.4                 13,5
> 2011 NA                     23.5                 14.5
> 2012 11.3                   NA                    23.4
> 
> >plot(Cmean,Nmean,col=c("green","red","blue"),....)
> 
> #works fine, gives a plot with data points coloured by "Year"
> 
> >text(Cmean,Nmean,labels=levels(Species),cex=.7,adj=c(-.1,-.6))
> 
> #does not work, mixes up the labels, have tried 
Species<-as.factor(Species)
> and switched tapply(d15N,list(Year,Species),mean)
> to-->tapply(d15N,list(Species,Year),mean) which gives the Years as 
column
> names but does not produce a better plot. The
> 
> I cannot find the error, any idea what i am doing wrong????
> Thanks sooo much

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to