[R] How to graph categorical data by percent

arun smartpink111 at yahoo.com
Thu Dec 5 21:11:41 CET 2013


Hi,
Try:
dat1 <- structure(list(Site = c("Downstream", "Downstream", "Downstream", 
"Downstream", "Downstream", "Downstream", "Downstream", "Downstream", 
"Downstream", "Midstream", "Midstream", "Midstream", "Midstream", 
"Midstream", "Upstream", "Upstream", "Upstream", "Upstream", 
"Upstream", "Upstream", "Upstream", "Upstream", "Upstream", "Upstream", 
"Upstream", "Upstream", "Upstream", "Upstream"), Intersex = c("no", 
"no", "no", "no", "yes", "no", "yes", "no", "no", "yes", "no", 
"no", "no", "no", "yes", "yes", "yes", "yes", "yes", "no", "no", 
"yes", "yes", "no", "yes", "no", "yes", "yes")), .Names = c("Site", 
"Intersex"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", 
"27", "28"))


res1 <- aggregate(Intersex~Site,data=dat1,function(x) (table(x)/length(x))*100)
 res1New <- do.call(`data.frame`,res1)
#or
library(plyr)
res2 <- ddply(dat1,.(Site),summarize, Percent=(table(Intersex)/length(Intersex))*100)
 

plot(Intersex.yes~as.numeric(Site),data=res1New,xaxt="n",xlab="Site",ylim=c(0,100),type="b")
axis(1,at=as.numeric(res1New$Site),levels(res1New$Site))


A.K.

Hi I have the following dataset: 

 Site Intersex 
1  Downstream       no 
2  Downstream       no 
3  Downstream       no 
4  Downstream       no 
5  Downstream      yes 
6  Downstream       no 
7  Downstream      yes 
8  Downstream       no 
9  Downstream       no 
10  Midstream      yes 
11  Midstream       no 
12  Midstream       no 
13  Midstream       no 
14  Midstream       no 
15   Upstream      yes 
16   Upstream      yes 
17   Upstream      yes 
18   Upstream      yes 
19   Upstream      yes 
20   Upstream       no 
21   Upstream       no 
22   Upstream      yes 
23   Upstream      yes 
24   Upstream       no 
25   Upstream      yes 
26   Upstream       no 
27   Upstream      yes 
28   Upstream      yes 

I want to group the sites and calculate the percent of "yes" 
"no" for Intersex.  Then I want to plot the prevalence of intersex 
(basically plot the percent of "yes" for each site.  I'm stumped! 
 Thanks



More information about the R-help mailing list