Tinus Sonnekus
2012-Sep-11 17:52 UTC
[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@gmail.com [[alternative HTML version deleted]]
arun
2012-Sep-11 18:16 UTC
[R] 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 -- 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.
Rui Barradas
2012-Sep-11 18:37 UTC
[R] Maintaining specific order when using aggregate or change order on axis
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 >
William Dunlap
2012-Sep-11 18:47 UTC
[R] Maintaining specific order when using aggregate or change order on axis
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.
arun
2012-Sep-11 20:00 UTC
[R] Maintaining specific order when using aggregate or change order on axis
Hi, In that case, try this: #made up data 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 Coral??? 1401 Surface 0.216 3.270 4.240??? 7.726 Coral??? 1401??? Shallow 0.359 3.890 4.900??? 9.149 Coral??? 1401? Deep 0.278 1.860 1.210??? 2.748 Coral??? 1402 Surface 0.331 4.940 3.670??? 8.041 Coral??? 1402??? Fmax 0.493 4.840 3.750??? 8.953 Coral??? 1402? Below 0.186 0.486 0.214??? 0.776 ",sep="",header=TRUE,stringsAsFactors=FALSE) ?Coral2<-with(Coral1 , aggregate(cbind(Pico, Nano, Micro), list(Depth),FUN=mean)) Coral2[order(order(c("Surface","Shallow","Deep","Fmax","Below"))),] #? Group.1????? Pico???? Nano??? Micro #5 Surface 0.2485000 3.905000 3.955000 #4 Shallow 0.3590000 3.890000 4.900000 #2??? Deep 0.2780000 1.860000 1.210000 #3??? Fmax 0.5716667 4.356667 4.133333 #1?? Below 0.1800000 0.744000 0.546000 A.K. ________________________________ From: Tinus Sonnekus <tsonnekus at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, September 11, 2012 2:36 PM Subject: Re: [R] Maintaining specific order when using aggregate or change order on axis Hi, Thanks for the reply. I know the order is abit confusing, but here is abit more background info to the depths. For the various depths the ships's ctd sampled.? surface = ctd @ surface e.g 5m shallow = between surface and fmax e.g. 25m deep =?between surface and fmax, but deeper than shallow e.g 50m fmax = ctd sampled at maximum flouresence ?e.g 100m below = below the fmax e.g. 200m The problem I can't really assign a?specific?depth value to each as fmax changed depth depending on location. If fmax was very shallow then "deep" and "shallow" was dropped from the?predetermined sampling depths. Hope this helps explain what I'm trying to do with the?ordering?of the data. Thanks for all your help. Been a steep learning curve this last week with R Shot Tinus On Tue, Sep 11, 2012 at 8:16 PM, arun <smartpink111 at yahoo.com> wrote: 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. > >-- 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