Hi, I want to compare to barplots with same horizontal axis limits. x=c(55,56,57,58,59,60,60,60,61,62,63,64,65) y=c(35,40,45,50,55,60,60,60,65,70,75,80,85) par(mfrow=c(2,1)) barplot(table(x),xlim=c(35,85)) barplot(table(y),xlim=c(35,85)) par(mfrow=c(1,1)) But the bars disappear. <http://r.789695.n4.nabble.com/file/n4676717/Rplot.png> Any suggestions? -- View this message in context: http://r.789695.n4.nabble.com/xlim-with-barplot-tp4676717.html Sent from the R help mailing list archive at Nabble.com.
On 09/23/2013 12:55 PM, David Arnold wrote:> Hi, > > I want to compare to barplots with same horizontal axis limits. > > x=c(55,56,57,58,59,60,60,60,61,62,63,64,65) > y=c(35,40,45,50,55,60,60,60,65,70,75,80,85) > > par(mfrow=c(2,1)) > barplot(table(x),xlim=c(35,85)) > barplot(table(y),xlim=c(35,85)) > par(mfrow=c(1,1)) > > But the bars disappear. > > <http://r.789695.n4.nabble.com/file/n4676717/Rplot.png> > > Any suggestions? >Hi David, The first suggestion is: par("usr") [1] -0.32 13.72 -0.03 3.00 Specifying the x limits as above means that the bars are floating somewhere off to the left of the plot and thus not visible. You are mistaking the labels of the bars for their position. Now, having admonished you like some grumpy old school teacher, I suppose I should do something useful: barplot(tabulate(x,nbins=85)[35:85]) barplot(tabulate(y,nbins=85)[35:85]) This is an underhanded trick to line up the bars as I think you want them. I suppose you want x labels as well: barpos<-barplot(tabulate(x,nbins=85)[35:85],names.arg=xylabels) axis(1,at=barpos[c(6,16,26,36,46)],labels=c(40,50,60,70,80)) barplot(tabulate(y,nbins=85)[35:85],names.arg=xylabels) axis(1,at=barpos[c(6,16,26,36,46)],labels=c(40,50,60,70,80)) Jim
You could try ggplot() as well. library(ggplot2) library(gridExtra) library(plyr) x1<- count(x) ?y1<- count(y) p1<-ggplot(x1,aes(x=x,y=freq))+geom_bar(stat="identity",colour="gray",fill="red")+xlim(c(35,85))+ theme_bw()+ theme(axis.line=element_line(colour="black"), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.border=element_blank(),panel.background=element_blank()) ?p2<-ggplot(y1,aes(x=x,y=freq))+geom_bar(stat="identity",colour="gray",fill="blue")+xlim(c(35,85)) +theme_bw()+ theme(axis.line=element_line(colour="black"), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.border=element_blank(),panel.background=element_blank()) ?grid.arrange(p1,p2,nrow=2) A.K. ----- Original Message ----- From: David Arnold <dwarnold45 at suddenlink.net> To: r-help at r-project.org Cc: Sent: Sunday, September 22, 2013 10:55 PM Subject: [R] xlim with barplot Hi, I want to compare to barplots with same horizontal axis limits. x=c(55,56,57,58,59,60,60,60,61,62,63,64,65) y=c(35,40,45,50,55,60,60,60,65,70,75,80,85) par(mfrow=c(2,1)) barplot(table(x),xlim=c(35,85)) barplot(table(y),xlim=c(35,85)) par(mfrow=c(1,1)) But the bars disappear. <http://r.789695.n4.nabble.com/file/n4676717/Rplot.png> Any suggestions? -- View this message in context: http://r.789695.n4.nabble.com/xlim-with-barplot-tp4676717.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.