jon waterhouse
2012-Mar-02 19:52 UTC
[R] Spacing of text does not match spacing of bars in barplot
I have a very standard barplot. My labels are too long to be printed horizontally under each bar, so I am using text to put the labels on a 45 degree slant. However, the labels are spaced more narrowly than the bars, so on an 8 vertical bar plot, the end of the eighth label is lined up with the seventh bar. Preferably I don't want to do every text label separately (I'm having this problem on all the graphs where I'm using text() Using Windows and version 2.14.1 X2sum <- c(42.6, 3.6, 1.8, 3.9, 12.1, 14.3, 14.6 ,28.4) X2.labels <- c("No earnings", "Less than $5000/year", "$5K to $10K" , "$10K to $15K" , "$ 15K to $20K" , "$20K to $25K" , "$25K to $30K" , "Over $30K" ) barplot(X2sum) text(1:8, par("usr")[3] - 0.5, srt = 45, adj = 1, labels =X2.labels, xpd TRUE) Thanks, Jon -- View this message in context: http://r.789695.n4.nabble.com/Spacing-of-text-does-not-match-spacing-of-bars-in-barplot-tp4439635p4439635.html Sent from the R help mailing list archive at Nabble.com.
William Dunlap
2012-Mar-02 19:59 UTC
[R] Spacing of text does not match spacing of bars in barplot
The return value of barplot contains the locations of the bars that it just drew. Use that instead of 1:8 when you draw the text: > barCenters <- barplot(X2sum) > text(barCenters, par("usr")[3] - 0.5, srt = 45, adj = 1, labels =X2.labels, xpd = TRUE) Look at help(barplot) for details. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of jon waterhouse > Sent: Friday, March 02, 2012 11:52 AM > To: r-help at r-project.org > Subject: [R] Spacing of text does not match spacing of bars in barplot > > I have a very standard barplot. My labels are too long to be printed > horizontally under each bar, so I am using text to put the labels on a 45 > degree slant. > > However, the labels are spaced more narrowly than the bars, so on an 8 > vertical bar plot, the end of the eighth label is lined up with the seventh > bar. > > Preferably I don't want to do every text label separately (I'm having this > problem on all the graphs where I'm using text() > > Using Windows and version 2.14.1 > > X2sum <- c(42.6, 3.6, 1.8, 3.9, 12.1, 14.3, 14.6 ,28.4) > X2.labels <- c("No earnings", "Less than $5000/year", "$5K to $10K" , "$10K > to $15K" , "$ 15K to $20K" , "$20K to $25K" , "$25K to $30K" > , "Over $30K" ) > > barplot(X2sum) > text(1:8, par("usr")[3] - 0.5, srt = 45, adj = 1, labels =X2.labels, xpd > TRUE) > > Thanks, > > Jon > > > -- > View this message in context: http://r.789695.n4.nabble.com/Spacing-of-text-does-not-match-spacing-of- > bars-in-barplot-tp4439635p4439635.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.
Marc Schwartz
2012-Mar-02 20:01 UTC
[R] Spacing of text does not match spacing of bars in barplot
On Mar 2, 2012, at 1:52 PM, jon waterhouse wrote:> I have a very standard barplot. My labels are too long to be printed > horizontally under each bar, so I am using text to put the labels on a 45 > degree slant. > > However, the labels are spaced more narrowly than the bars, so on an 8 > vertical bar plot, the end of the eighth label is lined up with the seventh > bar. > > Preferably I don't want to do every text label separately (I'm having this > problem on all the graphs where I'm using text() > > Using Windows and version 2.14.1 > > X2sum <- c(42.6, 3.6, 1.8, 3.9, 12.1, 14.3, 14.6 ,28.4) > X2.labels <- c("No earnings", "Less than $5000/year", "$5K to $10K" , "$10K > to $15K" , "$ 15K to $20K" , "$20K to $25K" , "$25K to $30K" > , "Over $30K" ) > > barplot(X2sum) > text(1:8, par("usr")[3] - 0.5, srt = 45, adj = 1, labels =X2.labels, xpd > TRUE) > > Thanks, > > JonRead ?barplot and take note of the Value section: A numeric vector (or matrix, when beside = TRUE), say mp, giving the coordinates of all the bar midpoints drawn, useful for adding to the graph. If beside is true, use colMeans(mp) for the midpoints of each group of bars, see example. Thus: mp <- barplot(X2sum) text(mp, ...) You correctly read the R FAQ on the matter, but that example uses plot() rather than barplot(). The midpoints of the bars are not at integer values. HTH, Marc Schwartz