Ah, I see. You could try something like this ... create a list of your data at the outset, then just use the for() loop to create the plots.
Jean # fake data with two Seamounts ... one with a missing depth SChla <- structure(list(Seamount = c("Atlantis", "Atlantis", "Atlantis", "Atlantis", "Atlantis", "Atlantis", "Neptune", "Neptune", "Neptune", "Neptune", "Neptune"), Station = c(1217L, 1217L, 1217L, 1217L, 1217L, 1225L, 1217L, 1217L, 1217L, 1217L, 1225L), Depth = c("Surface", "Shallow", "Deep", "Fmax", "Below", "Surface", "Surface", "Shallow", "Fmax", "Below", "Surface"), Pico = c(0.0639, 0.0305, 0.166, 0.174, 0.0235, 0.0764, 0.0639, 0.0305, 0.174, 0.0235, 0.0764), Nano = c(0.156, 0.125, 0.356, 0.52, 0.0486, 0.149, 0.156, 0.125, 0.52, 0.0486, 0.149), Micro = c(0.0398, 0.074, 0.0734, 0.183, 0.0242, 0.0333, 0.0398, 0.074, 0.183, 0.0242, 0.0333 ), Total_Ch = c(0.2597, 0.2295, 0.5954, 0.877, 0.0963, 0.2587, 0.2597, 0.2295, 0.877, 0.0963, 0.2587), dbar = c(2.082, 24.524, 49.573, 79.595, 199.067, 0.409, 2.082, 24.524, 79.595, 199.067, 0.409)), .Names = c("Seamount", "Station", "Depth", "Pico", "Nano", "Micro", "Total_Ch", "dbar"), row.names = c("1", "2", "3", "4", "5", "6", "11", "21", "41", "51", "61"), class = "data.frame") library(plotrix) sm.split <- split(SChla, SChla$Seamount) sm.means <- lapply(sm.split, with, aggregate(cbind(Pico, Nano, Micro, Total_Ch), list(Depth=Depth), FUN=mean)) colz <- c("light green", "green", "dark green") for (i in seq(sm.means)) { sub <- sm.means[[i]] yy <- t(sub[, 2:4]) dimnames(yy)[[2]] <- sub$Depth # I used windows() instead of jpeg() so I could quickly see the results on my PC windows() barp(yy, ylim=c(0, max(sub$Total_Ch + 0.1)), col=colz, xlab="Depth (m)", legend.pos=0.6, ylab=expression(paste("Chlorophyll ", italic(" a ") ~~ (mu*g ~ l^{-1}))), names.arg=sub$Depth) points(sm.means$Total_Ch, pch = 19) lines(sm.means$Total_Ch) title(names(sm.means)[i]) legend("topleft", c("Pico", "Nano", "Micro", "Total"), bty="n", col=c(colz, "black"), pch=c(15, 15, 15, 19), lty=c(0, 0, 0, 1)) } On Fri, Jun 21, 2013 at 11:23 AM, Tinus Sonnekus <tsonne...@gmail.com>wrote: > Hi Jean, > > Thanks for the help. Yes, it does work but, I need to somehow get rid of > the "deep" category for the three seamounts with out it. Any ideas? > > Regards, > > > > > On 21 June 2013 18:16, Adams, Jean <jvad...@usgs.gov> wrote: > >> Does it work if you ignore NAs when calculating the max? >> >> ylim=c(0,max(sm.mean$Total_Ch + 0.1, na.rm=TRUE)) >> >> Jean >> >> >> On Fri, Jun 21, 2013 at 7:30 AM, Tinus Sonnekus <tsonne...@gmail.com>wrote: >> >>> Hi all, >>> >>> So this code works for three of the six seamounts. The problem I am >>> encountering is the fact that some of the seamounts doesn't have all the >>> depth categories (i.e missing the deep category) and thus gives me this >>> error "Error in plot.window(...) : need finite 'ylim' values." >>> >>> How can I fix this so that if a depth category is missing that it won't >>> result in an error. If I change ylim=c(0,max(sm.mean$Total_Ch + 0.1)), to >>> something like this ylim=c(0,6)), then it works but the I don't like >>> scale. >>> >>> Please help and thank you. >>> >>> > head(SChla) >>> Seamount Station Depth Pico Nano Micro Total_Ch dbar >>> 1 Atlantis 1217 Surface 0.0639 0.1560 0.0398 0.2597 2.082 >>> 2 Atlantis 1217 Shallow 0.0305 0.1250 0.0740 0.2295 24.524 >>> 3 Atlantis 1217 Deep 0.1660 0.3560 0.0734 0.5954 49.573 >>> 4 Atlantis 1217 Fmax 0.1740 0.5200 0.1830 0.8770 79.595 >>> 5 Atlantis 1217 Below 0.0235 0.0486 0.0242 0.0963 199.067 >>> 6 Atlantis 1225 Surface 0.0764 0.1490 0.0333 0.2587 0.409 >>> >>> >>> # Get the unique seamounts >>> as.character(unique(SChla$Seamount)) >>> sm <- as.character(unique(SChla$Seamount)) >>> >>> for ( i in sm[1:6]) { >>> SM.mean <- subset(SChla,Seamount==i, >>> select=c(Seamount, Station, Depth, Pico, Nano, Micro, Total_Ch)) >>> sm.mean <- with(SM.mean, aggregate(cbind(Pico, Nano, Micro, Total_Ch), >>> list(Depth),FUN=mean)) >>> sm.mean <- >>> sm.mean[order(order(c("Surface","Shallow","Deep","Fmax","Below"))),] >>> yy <- matrix(c(sm.mean$Pico,sm.mean$Nano, sm.mean$Micro),5,3,byrow=FALSE, >>> dimnames = list(c("Below", "Deep", "Fmax", "Shallow", "Surface"), >>> c("Pico", "Nano", "Micro"))) >>> yy <- t(yy) >>> >>> jpeg(filename = paste(i," >>> Seamount.jpg"),res=300,width=15,height=10,units="cm") >>> >>> barp(yy , ylim=c(0,max(sm.mean$Total_Ch + 0.1)), col=c("light >>> green","green","dark green"), >>> xlab="Depth (m)", >>> legend.pos = 0.6, >>> ylab= expression (paste("Chlorophyll ", italic(" a ") ~~ (mu*g ~ >>> l^{-1}))),, >>> names.arg=c("Surface","Shallow","Deep","Fmax","Below")) >>> points(sm.mean$Total_Ch, pch = 19) >>> lines(sm.mean$Total_Ch) >>> title(i) >>> legend("topleft",c("Pico","Nano","Micro", "Total"),bty="n", >>> col=c("light green","green","dark green","black"), >>> pch=c(15,15,15,19,-1), >>> lty = c(0,0,0,1)) >>> } >>> dev.off() >>> >>> >>> >>> Regards, >>> Tinus >>> -- >>> M.J. Sonnekus >>> PhD Candidate (The Phytoplankton of the southern Agulhas Current Large >>> Marine Ecosystem (ACLME)) >>> Department of Botany >>> South Campus >>> Nelson Mandela Metropolitan University >>> PO Box 77000 >>> Port Elizabeth >>> South Africa >>> 6031 >>> >>> Cell: 082 080 9638 >>> E-mail: tsonne...@gmail.com >>> >>> [[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. >>> >> >> > > > -- > M.J. Sonnekus > PhD Candidate (The Phytoplankton of the southern Agulhas Current Large > Marine Ecosystem (ACLME)) > Department of Botany > South Campus > Nelson Mandela Metropolitan University > PO Box 77000 > Port Elizabeth > South Africa > 6031 > > Cell: 082 080 9638 > E-mail: tsonne...@gmail.com > [[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.