Hello, I'm trying to do bar plot where 'sex' will be the category axis and 'occupation' will represent the bars and the clusters will represent the mean 'income'. sex occupation income 1 female j 12 2 male b 34 3 male j 22 4 female j 54 5 male b 33 6 female b 67 7 male j 89 8 male b 65 9 female j 45 10 male j 32 I can do bar plot where sex is the category axis and the clusters represent 'occupation'. the code is-> t<- table(data$sex,data$occupation) > barplot(f)and the barplot where the category axis is 'sex' and the cluster represent the mean income and median income. The code is -> mean=tapply(data$income,data$sex,mean) > meanfemale male 38.66667 46.50000> median=tapply(data$income,data$sex,median) > medianfemale male 22.5 49.5> r=rbind(mean,median) > rfemale male mean 38.66667 46.5 median 22.50000 49.5> par(fg='red',cex=1.2) > barplot(r,col=c('green','yellow'),cex.axis=1.2,col.axis='red',ylim=c(0,120)But how can I make 'occupation'' to nest inside 'sex' and then the cluster to represent the mean income? For example I am attaching a pdf plot that is produced by SPSS. Thank you. -- Tanvir Khan MS Student Institute Of Statistical Research & Training University Of Dhaka tkhan1 at isrt.ac.bd khan.tanvir_007 at ymail.com -------------- next part -------------- A non-text attachment was scrubbed... Name: OUTPUT.pdf Type: application/pdf Size: 2744 bytes Desc: not available URL: <stat.ethz.ch/pipermail/r-help/attachments/20100910/91d91f81/attachment.pdf>
On Sep 10, 2010, at 11:38 , Tanvir Khan wrote:> Hello, > I'm trying to do bar plot where 'sex' will be the category axis and > 'occupation' will represent the bars and the clusters will represent > the mean 'income'. > > sex occupation income > 1 female j 12 > 2 male b 34 > 3 male j 22 > 4 female j 54 > 5 male b 33 > 6 female b 67 > 7 male j 89 > 8 male b 65 > 9 female j 45 > 10 male j 32 > > I can do bar plot where sex is the category axis and the clusters > represent 'occupation'. > the code is- > >> t<- table(data$sex,data$occupation) >> barplot(f) > > and the barplot where the category axis is 'sex' and the cluster > represent the mean income and median income. The code is - >> mean=tapply(data$income,data$sex,mean) >> mean > female male > 38.66667 46.50000 >> median=tapply(data$income,data$sex,median) >> median > female male > 22.5 49.5 >> r=rbind(mean,median) >> r > female male > mean 38.66667 46.5 > median 22.50000 49.5 >> par(fg='red',cex=1.2) >> barplot(r,col=c('green','yellow'),cex.axis=1.2,col.axis='red',ylim=c(0,120) > > But how can I make 'occupation'' to nest inside 'sex' and then the > cluster to represent the mean income? > For example I am attaching a pdf plot that is produced by SPSS.(Please use sensible Subject:, those of us with threading mail programs see it mixed in with oodles of old and irrelevant posts.) I think you are just looking for some of this inc.by.sex.occ <- with(data, tapply(income, list(sex,occupation), mean)) barplot(inc.by.sex.occ) barplot(inc.by.sex.occ, beside=T) barplot(t(inc.by.sex.occ)) barplot(t(inc.by.sex.occ),beside=T) (or, of course, reverse the order in the list() rather than transpose the result) -- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Have a look at the ggplot2 package install.packages("ggplot2") library(ggplot2) ggplot(data, aes(x = sex, y = income, fill = occupation)) + geom_bar(position = "dodge") Have a look at had.co.nz/ggplot2 for more information and examples. HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek team Biometrie & Kwaliteitszorg Gaverstraat 4 9500 Geraardsbergen Belgium Research Institute for Nature and Forest team Biometrics & Quality Assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey> -----Oorspronkelijk bericht----- > Van: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] Namens Tanvir Khan > Verzonden: vrijdag 10 september 2010 11:39 > Aan: r-help at r-project.org > Onderwerp: [R] (no subject) > > Hello, > I'm trying to do bar plot where 'sex' will be the category > axis and 'occupation' will represent the bars and the > clusters will represent the mean 'income'. > > sex occupation income > 1 female j 12 > 2 male b 34 > 3 male j 22 > 4 female j 54 > 5 male b 33 > 6 female b 67 > 7 male j 89 > 8 male b 65 > 9 female j 45 > 10 male j 32 > > I can do bar plot where sex is the category axis and the > clusters represent 'occupation'. > the code is- > > > t<- table(data$sex,data$occupation) > > barplot(f) > > and the barplot where the category axis is 'sex' and the > cluster represent the mean income and median income. The code is - > > mean=tapply(data$income,data$sex,mean) > > mean > female male > 38.66667 46.50000 > > median=tapply(data$income,data$sex,median) > > median > female male > 22.5 49.5 > > r=rbind(mean,median) > > r > female male > mean 38.66667 46.5 > median 22.50000 49.5 > > par(fg='red',cex=1.2) > > > barplot(r,col=c('green','yellow'),cex.axis=1.2,col.axis='red',ylim=c(0 > > ,120) > > But how can I make 'occupation'' to nest inside 'sex' and > then the cluster to represent the mean income? > For example I am attaching a pdf plot that is produced by SPSS. > > Thank you. > > > -- > Tanvir Khan > MS Student > Institute Of Statistical Research & Training University Of > Dhaka tkhan1 at isrt.ac.bd khan.tanvir_007 at ymail.com >Druk dit bericht a.u.b. niet onnodig af. Please do not print this message unnecessarily. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
On 09/10/2010 07:38 PM, Tanvir Khan wrote:> Hello, > I'm trying to do bar plot where 'sex' will be the category axis and > 'occupation' will represent the bars and the clusters will represent > the mean 'income'. > > sex occupation income > 1 female j 12 > 2 male b 34 > 3 male j 22 > 4 female j 54 > 5 male b 33 > 6 female b 67 > 7 male j 89 > 8 male b 65 > 9 female j 45 > 10 male j 32 > > I can do bar plot where sex is the category axis and the clusters > represent 'occupation'. > the code is- > >> t<- table(data$sex,data$occupation) >> barplot(f) > > and the barplot where the category axis is 'sex' and the cluster > represent the mean income and median income. The code is - >> mean=tapply(data$income,data$sex,mean) >> mean > female male > 38.66667 46.50000 >> median=tapply(data$income,data$sex,median) >> median > female male > 22.5 49.5 >> r=rbind(mean,median) >> r > female male > mean 38.66667 46.5 > median 22.50000 49.5 >> par(fg='red',cex=1.2) >> barplot(r,col=c('green','yellow'),cex.axis=1.2,col.axis='red',ylim=c(0,120) > > But how can I make 'occupation'' to nest inside 'sex' and then the > cluster to represent the mean income? > For example I am attaching a pdf plot that is produced by SPSS. >Hi Tanvir, I think you may be looking for barNest: examp<-data.frame(sex=sample(c("Female","Male"),100,TRUE), position=sample(c("Clerical","Custodial","Manager"),100,TRUE), income=rnorm(100,30000,5000)) # just show the final bars barNest(income~sex+position,examp, col=list("gray",c("pink","lightblue"),c("blue","green","tan"))) # show the entire nest of bars barNest(income~sex+position,examp,showall=TRUE, col=list("gray",c("pink","lightblue"),c("blue","green","tan"))) Jim