Skip to content
Prev 305244 / 398513 Next

Maintaining specific order when using aggregate or change order on axis

Hi,
The order you mentioned is confusing.? The given data shows depth to be increasing from Below,Surface,Fmax. 

This might help you in ordering.
Coral1<-read.table(text="
Seamount Station? Depth? Pico? Nano Micro Total_Ch
Coral??? 1401 Surface 0.216 3.270 4.240??? 7.726
Coral??? 1401??? Fmax 0.359 3.890 4.900??? 9.149
Coral??? 1401? Below 0.178 1.360 1.210??? 2.748
Coral??? 1402 Surface 0.231 4.140 3.670??? 8.041
Coral??? 1402??? Fmax 0.863 4.340 3.750??? 8.953
Coral??? 1402? Below 0.176 0.386 0.214??? 0.776
",sep="",header=TRUE,stringsAsFactors=TRUE)

?Coral2<-with(Coral1 , aggregate(cbind(Pico, Nano, Micro), list(Depth),FUN=mean))
?Coral2[order(Coral2$Pico,Coral2$Nano,Coral2$Micro),]
?# Group.1?? Pico? Nano Micro
#1?? Below 0.1770 0.873 0.712
#3 Surface 0.2235 3.705 3.955
#2??? Fmax 0.6110 4.115 4.325

#reverse order

?Coral2[rev(order(Coral2$Pico,Coral2$Nano,Coral2$Micro)),]
?# Group.1?? Pico? Nano Micro
#2??? Fmax 0.6110 4.115 4.325
#3 Surface 0.2235 3.705 3.955
#1?? Below 0.1770 0.873 0.712


A.K.




----- Original Message -----
From: Tinus Sonnekus <tsonnekus at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, September 11, 2012 1:52 PM
Subject: [R] Maintaining specific order when using aggregate or change order on axis

Hi All,

I'm using the following code to produce some stacked bar graphs.

*setwd("C:\\Users\\Tinus\\Documents\\NMMU\\R\\Seamounts")*
*SChla <- read.csv("SM_Chla_data.csv")*
*
*
*#Extract mean values from data file*
*
*
*Coral <- SChla[185:223,] #Reduce SChla to Coral only*
*coral <- with(Coral , aggregate(cbind(Pico, Nano, Micro),
list(Depth),FUN=mean))*
*rownames(coral) <- coral[,1] *
*coral <- t(coral[,-1]) # Remove Station col and transpose*

*barplot(coral, main="Size fractionated Chl a for Coral", *
* ylab = "Coral", xlim = c(0,8), horiz = TRUE,*
* xlab = expression ("Chlorophyll a " ~~ (mu*g ~ l^{-1})), *
* col =c("light green", "green", "dark green"),*
* legend = rownames(coral))*

Here is the *head(Coral) *before station col removal and t()
? ? Seamount Station?  Depth? Pico? Nano Micro Total_Ch
185? ? Coral? ? 1401 Surface 0.216 3.270 4.240? ? 7.726
186? ? Coral? ? 1401? ? Fmax 0.359 3.890 4.900? ? 9.149
187? ? Coral? ? 1401?  Below 0.178 1.360 1.210? ? 2.748
188? ? Coral? ? 1402 Surface 0.231 4.140 3.670? ? 8.041
189? ? Coral? ? 1402? ? Fmax 0.863 4.340 3.750? ? 8.953
190? ? Coral? ? 1402?  Below 0.176 0.386 0.214? ? 0.776

So I use the same code for the six other seamounts. The depth has
the following values? surface, shallow, deep, fmax and below . These are in
order as you go down the water column. The problem is when I use the *coral
<- with(Coral , aggregate(cbind(Pico, Nano, Micro), list(Depth),FUN=mean)) *the
depth values gets stored in ascending values.

head(coral)
? Group.1? ? ? Pico? ? ? Nano? ?  Micro
1?  Below 0.1652727 0.8610909 0.7227273
2? ? Deep 0.1480000 1.1700000 1.1000000
3? ? Fmax 0.3067273 3.1845455 3.2245455
4 Shallow 0.2617500 1.8242500 1.8637500
5 Surface 0.1693333 2.7083333 2.7858333

I would like to maintain the order surface, shallow, deep, fmax and below. *Or
*if there is a way I can rearrange the yaxis in the order surface, shallow,
deep, fmax and below.

Any suggestions?

Thank you
Tinus