Dear sir, I am trying to create a barplot for two propotions, with proportions successful on the y and age on the x. So my data is in the formate of one vector containing 0s and 1s (i.e.successful or not) with a corresponding two level vector of age. success age 1 old 0 young 0 young 1 old Howeber i cannot find a way to stop r stacking the data using the code: plot(successfulYN~maleage) as it ends up showing both proportions as i only want the proportions of success to show. also entering it as a table results in x axis being removed maybe though this is the only way could you please offer any help? Grant McDoanld
On Thu, 13 Aug 2009, Mcdonald, Grant wrote:> Dear sir, > > I am trying to create a barplot for two propotions, with proportions successful on the y and age on the x. So my data is in the formate of one vector containing 0s and 1s (i.e.successful or not) with a corresponding two level vector of age. > > success age > 1 old > 0 young > 0 young > 1 old > > Howeber i cannot find a way to stop r stacking the data using the code: plot(successfulYN~maleage) as it ends up showing both proportions as i only want the proportions of success to show. > also entering it as a table results in x axis being removed maybe though this is the only way > could you please offer any help?A reproducible example would have been helpful (as the posting guide explains). Using the data from ?spineplot: treatment <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2), labels = c("placebo", "treated")) improved <- factor(rep(c(1, 2, 3, 1, 2, 3), c(29, 7, 7, 13, 7, 21)), levels = c(1, 2, 3), labels = c("none", "some", "marked")) I think you have a plot which is similar to plot(treatment ~ improved) showing P(treatment | improved) on the y-axis and P(improved) on the x-axis. And you want the same y-axis but an x-axis with equal widths on the x-axis, right? Then you can do ## store the underlying contingency table tab <- plot(treatment ~ improved) tab prop.table(tab, 1) ## plotting with P(improved) x-axis spineplot(tab) ## plotting with equi-sized bars spineplot(prop.table(tab, 1)) (Note that the order of the variables would typically be exchanged but this ordering makes the difference between the different plots more obvious.) hth, Z> Grant McDoanld > > ______________________________________________ > 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. > >
________________________________________ From: Mcdonald, Grant Sent: 13 August 2009 13:08 To: r-help at R-project.org Subject: R graphics Dear sir, I am trying to create a barplot showing propotions, So my data is in the format of a vector containing 0s and 1s (i.e.successful or not) with a corresponding vector with two catogorical levels. success age 1 old 0 young 0 young 1 old I want to show a bar plot with a ylim=c(0,1) and an x-axis showing the two ages and the proportion of success only as filled bars. after making success a factor and plotting against age using: plot(success~age) the graphic shows age on the x and proportion on the yaxis as i want. However for each age it shows the total proportion split into both success and failures. I would however just like to show the prop successes without the failures stajed on top. data frame is below: successful maleage 1 O 1 Y 1 Y 1 Y 1 O 1 O 1 Y 1 O 1 Y 1 O 1 O 1 Y 1 O 1 Y 1 Y 1 Y 1 O 1 O 1 Y 1 Y 1 O 1 Y 1 O 1 Y 1 Y 1 Y 1 O 1 Y 1 Y 1 O 1 Y 1 O 1 O 1 Y 1 O 1 Y 1 O 1 Y 1 Y 1 O 1 Y 1 Y 1 O 1 Y 1 Y 1 Y 1 O 1 O 1 Y 1 O 1 Y 1 Y 1 Y 1 Y 1 Y 1 Y 1 Y 1 Y 1 Y 1 O 1 Y 1 O 1 Y 1 Y 1 O 1 O 1 Y 1 Y 1 O 1 Y 1 O 1 Y 1 Y 1 Y 1 O 1 Y 1 O 1 Y 1 Y 1 Y 1 O 1 Y 1 O 1 Y 1 Y 1 O 1 Y 1 Y 1 O 1 O 1 Y 1 Y 1 Y 1 O 0 Y NA O 0 O NA Y 1 Y 1 O NA O NA Y 0 O NA Y 0 O NA Y 1 Y 1 O 0 O 0 Y NA O 0 Y NA O 0 Y 0 Y NA O NA O NA O NA Y NA Y 0 O NA O NA Y NA O 1 O 0 O 0 Y NA O NA Y 1 O 0 O 0 O 0 O NA Y 0 O 0 Y NA Y 0 O NA O 0 Y NA O 1 O 0 Y NA O NA O NA O 0 O 0 O 0 O 0 Y 0 O 0 O 0 O 0 Y NA O 0 Y 0 O NA Y NA O 0 O NA Y NA O 0 O NA O NA O 0 O NA Y NA O NA Y 0 O NA O 1 Y NA Y NA O 1 O 1 Y 0 O 0 Y 0 O 0 Y 0 Y 1 Y
On Thu, 13 Aug 2009, Mcdonald, Grant wrote:> > ________________________________________ > From: Mcdonald, Grant > Sent: 13 August 2009 13:08 > To: r-help at R-project.org > Subject: R graphics > > Dear sir, > > I am trying to create a barplot showing propotions, > > So my data is in the format of a vector containing 0s and 1s (i.e.successful or not) with a corresponding vector with two catogorical levels. > > success age > 1 old > 0 young > 0 young > 1 old > > I want to show a bar plot with a ylim=c(0,1) and an x-axis showing the two ages and the proportion of success only as filled bars. > > after making success a factor and plotting against age using: plot(success~age) > > the graphic shows age on the x and proportion on the yaxis as i want. However for each age it shows the total proportion split into both success and failures. I would however just like to show the prop successes without the failures stajed on top.You can play around with the graphical parameters of ?spineplot. For example, I copied your data into a "dat.txt" file and read the data via ## read data dat <- read.table("dat.txt", header = TRUE) ## transform labels dat$successful <- factor(dat$successful, levels = 1:0, labels c("success", "failure")) dat$maleage <- factor(dat$maleage, labels = c("old", "young")) And then you can produce various types of plots changing color, labeling etc. ## simple spine plot plot(successful ~ maleage, data = dat) ## hide failures and suppress y-axis labels plot(successful ~ maleage, data = dat, col = gray(c(0.3, 1)), border = "transparent", yaxlabels = "") Or you can also use barplot() and create this by hand: ## create table tab <- xtabs(~ maleage + successful, data = dat) ## simple barplot of success proportions barplot(prop.table(tab, 1)[,1]) ## barplot with proportional x-axis and labeling etc. barplot(prop.table(tab, 1)[,1], width = margin.table(tab, 1), col = gray(0.3), space = 0.1, ylim = c(0, 1), ylab = "proportion of succsess") hth, Z> data frame is below: > > successful maleage > 1 O > 1 Y > 1 Y > 1 Y > 1 O > 1 O > 1 Y > 1 O > 1 Y > 1 O > 1 O > 1 Y > 1 O > 1 Y > 1 Y > 1 Y > 1 O > 1 O > 1 Y > 1 Y > 1 O > 1 Y > 1 O > 1 Y > 1 Y > 1 Y > 1 O > 1 Y > 1 Y > 1 O > 1 Y > 1 O > 1 O > 1 Y > 1 O > 1 Y > 1 O > 1 Y > 1 Y > 1 O > 1 Y > 1 Y > 1 O > 1 Y > 1 Y > 1 Y > 1 O > 1 O > 1 Y > 1 O > 1 Y > 1 Y > 1 Y > 1 Y > 1 Y > 1 Y > 1 Y > 1 Y > 1 Y > 1 O > 1 Y > 1 O > 1 Y > 1 Y > 1 O > 1 O > 1 Y > 1 Y > 1 O > 1 Y > 1 O > 1 Y > 1 Y > 1 Y > 1 O > 1 Y > 1 O > 1 Y > 1 Y > 1 Y > 1 O > 1 Y > 1 O > 1 Y > 1 Y > 1 O > 1 Y > 1 Y > 1 O > 1 O > 1 Y > 1 Y > 1 Y > 1 O > 0 Y > NA O > 0 O > NA Y > 1 Y > 1 O > NA O > NA Y > 0 O > NA Y > 0 O > NA Y > 1 Y > 1 O > 0 O > 0 Y > NA O > 0 Y > NA O > 0 Y > 0 Y > NA O > NA O > NA O > NA Y > NA Y > 0 O > NA O > NA Y > NA O > 1 O > 0 O > 0 Y > NA O > NA Y > 1 O > 0 O > 0 O > 0 O > NA Y > 0 O > 0 Y > NA Y > 0 O > NA O > 0 Y > NA O > 1 O > 0 Y > NA O > NA O > NA O > 0 O > 0 O > 0 O > 0 Y > 0 O > 0 O > 0 O > 0 Y > NA O > 0 Y > 0 O > NA Y > NA O > 0 O > NA Y > NA O > 0 O > NA O > NA O > 0 O > NA Y > NA O > NA Y > 0 O > NA O > 1 Y > NA Y > NA O > 1 O > 1 Y > 0 O > 0 Y > 0 O > 0 Y > 0 Y > 1 Y > > ______________________________________________ > 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. > >