An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110416/d6c20026/attachment.pl>
Rotating the x-axis labels of a barplot
5 messages · Josh B, Sascha Vieweg, Peter Ehlers +2 more
On 11-04-16 15:04, Josh B wrote:
Dear listserv, Here is my latest formatting problem. I would like to rotate the x-axis labels by 45 degrees on a _barplot_. Apparently this is slightly different from the example given in the R FAQ, which is for rotating the x-axis labels on a scatterplot (http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-create-rotated-axis-labels_003f). I have adapted that code, as best I could, and here is what I have come up with so far: x <- c(4, 1, 1, 1) barplot(x, space = 0) #the "space = 0" argument, above, just reflects my personal preference for having no space between the bars text(1:4, par("usr")[3] - 0.025, srt = 45, adj = 1, labels = c("< 750", "750-1000", "1000-1250", "> 1250"), xpd = TRUE, font = 2) The graph this generates is pretty close to what I want, but here's where I'm stuck: How do I move the x-axis labels to the _center_ of each bar? Right now, they line up with the right-side of each bar.
Try to set the parameter: adj=.5 (see ?par). HTH, *S*
Sascha Vieweg, saschaview at gmail.com
On 2011-04-16 09:36, Sascha Vieweg wrote:
On 11-04-16 15:04, Josh B wrote:
Dear listserv, Here is my latest formatting problem. I would like to rotate the x-axis labels by 45 degrees on a _barplot_. Apparently this is slightly different from the example given in the R FAQ, which is for rotating the x-axis labels on a scatterplot (http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-create-rotated-axis-labels_003f). I have adapted that code, as best I could, and here is what I have come up with so far: x<- c(4, 1, 1, 1) barplot(x, space = 0) #the "space = 0" argument, above, just reflects my personal preference for having no space between the bars text(1:4, par("usr")[3] - 0.025, srt = 45, adj = 1, labels = c("< 750", "750-1000", "1000-1250","> 1250"), xpd = TRUE, font = 2) The graph this generates is pretty close to what I want, but here's where I'm stuck: How do I move the x-axis labels to the _center_ of each bar? Right now, they line up with the right-side of each bar.
Try to set the parameter: adj=.5 (see ?par). HTH, *S*
That won't work (try it). Use text(1:4-0.5, ....) Peter Ehlers
On Apr 16, 2011, at 11:50 AM, Peter Ehlers wrote:
On 2011-04-16 09:36, Sascha Vieweg wrote:
On 11-04-16 15:04, Josh B wrote:
Dear listserv, Here is my latest formatting problem. I would like to rotate the x-axis labels by 45 degrees on a _barplot_. Apparently this is slightly different from the example given in the R FAQ, which is for rotating the x-axis labels on a scatterplot (http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-create-rotated-axis-labels_003f). I have adapted that code, as best I could, and here is what I have come up with so far: x<- c(4, 1, 1, 1) barplot(x, space = 0) #the "space = 0" argument, above, just reflects my personal preference for having no space between the bars text(1:4, par("usr")[3] - 0.025, srt = 45, adj = 1, labels = c("< 750", "750-1000", "1000-1250","> 1250"), xpd = TRUE, font = 2) The graph this generates is pretty close to what I want, but here's where I'm stuck: How do I move the x-axis labels to the _center_ of each bar? Right now, they line up with the right-side of each bar.
Try to set the parameter: adj=.5 (see ?par). HTH, *S*
That won't work (try it). Use text(1:4-0.5, ....) Peter Ehlers
Actually, the key is knowing that barplot() returns the bar midpoints (See the Value section of ?barplot).
Thus:
x <- c(4, 1, 1, 1)
mp <- barplot(x, space = 0)
text(mp, par("usr")[3] - 0.025, srt = 45, adj = 1,
labels = c("< 750", "750-1000", "1000-1250", "> 1250"),
xpd = TRUE, font = 2)
HTH,
Marc Schwartz
On 04/16/2011 11:04 PM, Josh B wrote:
Dear listserv, Here is my latest formatting problem. I would like to rotate the x-axis labels by 45 degrees on a _barplot_. Apparently this is slightly different from the example given in the R FAQ, which is for rotating the x-axis labels on a scatterplot (http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-create-rotated-axis-labels_003f). I have adapted that code, as best I could, and here is what I have come up with so far: x<- c(4, 1, 1, 1) barplot(x, space = 0) #the "space = 0" argument, above, just reflects my personal preference for having no space between the bars text(1:4, par("usr")[3] - 0.025, srt = 45, adj = 1, labels = c("< 750", "750-1000", "1000-1250","> 1250"), xpd = TRUE, font = 2) The graph this generates is pretty close to what I want, but here's where I'm stuck: How do I move the x-axis labels to the _center_ of each bar? Right now, they line up with the right-side of each bar.
Hi Josh, Have a look at the "barp" function in the plotrix package, in particular the "staxx" and "srt" arguments. Try adding "srt=45" to the last example. Jim