An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120911/dc1a25e2/attachment.pl>
Maintaining specific order when using aggregate or change order on axis
4 messages · Tinus Sonnekus, arun, Rui Barradas +1 more
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
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: tsonnekus at gmail.com ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Hello,
If you order your original (wanted) order and then order that result you
get what you want.
The example is with fake data but the idea is in the last two lines.
# Make up some data
set.seed(9210)
n <- 1e2
depth <- c('surface', 'shallow', 'deep', 'fmax', 'below')
dat <- data.frame(Depth = sample(depth, n, TRUE), Value = runif(n))
# This is sorted by increasing Depth
agg <- aggregate(Value ~ Depth, data = dat, FUN = mean)
ix <- order(depth) # First step
agg[order(ix), ] # inverse permutation
Hope this helps,
Rui Barradas
Em 11-09-2012 18:52, Tinus Sonnekus escreveu:
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
Right after importing your data with read.csv, make a factor out of your
Depth column and give it levels in the desired order. E.g.,
SChla$Depth <- factor(SChla$Depth, levels=c("Surface", "Shallow", "Deep", "Fmax", "Below"))
(Capitalization is important, "Below" is not the same as "below".)
To check that things were imported as expect do
table(SChla$Dept)
before and after the change. Only the order of the table entries should change.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
Of Tinus Sonnekus
Sent: Tuesday, September 11, 2012 10:52 AM
To: r-help at r-project.org
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
--
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: tsonnekus at gmail.com
[[alternative HTML version deleted]]
______________________________________________ R-help at 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.