On 02/25/2011 09:33 PM, Lucia Rueda wrote:
Hi,
I am using this loop
par(mfrow=c(3,3))
annos<-c(2001:2007,2009)
for (i in annos) {
t<-subset(masia,YEAR==i)
t$FAMILIA<-drop.levels(t$FAMILIA)
pie(table(t$FAMILIA),main=i)
}
To make piecharts of species composition among years (my data frame is
called "masia"). So I get 1 piechart of the families that we have found in
our survey each year. We don't have always the same families every year so I
added t$FAMILIA<-drop.levels(t$FAMILIA)
to the loop to avoid having those family levels that aren't there in some
specific years in the pie
The problem is that the color code changes and I get for example different
colors for the same families in different years.
If I group those families that I have less individuals together in a
category called "others" and I make a new column called "familia2" with
fewer levels so that every year I have all levels of familia2 in my species
composition I don't get the problem and all families have the same color
among years.
Does anybody know how to avoid the color code change for the families in the
loop. I know I can do it manually and give each family a color but I have
quite a lot of families so I'm wondering if there's any way to fix that some
other way.
Hi Lucia,
FAMILIA is probably a factor, therefore can be used as an index with
as.numeric(). So if you have a vector of colors for all the families in
your dataset, you could specify the color for each sector of the pie with:
# this gives you different colors for each family
family_colors<-1:length(levels(t$FAMILIA))
for(i in annos) {
t<-subset(masia,YEAR==i)
sector_index<-as.numeric(unique(t$FAMILIA))
pie(table(t$FAMILIA),main=i,col=family_colors[sector_index])
}
Can't try it at the moment, but it should be close.
Jim
[as.numeric(unique(t$FAMILIA[i]))] without dropping the levels (I think).
______________________________________________
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.