Hi all, - how can I do a barplot with rotated axis labels? I've seen the example for just a plot in the FAQ, but I'll missing the coordinates to plot my text at the right position beneath the bars. Is there any (easy?) solution? - how can I set the y-axis in a barplot to logarithmic scale? Many thanks in advance! Best Regards Tom --
On Thu, 2005-06-30 at 23:05 +0200, hulubu at gmx.de wrote:> Hi all, > > - how can I do a barplot with rotated axis labels? I've seen the example for > just a plot in the FAQ, but I'll missing the coordinates to plot my text at > the right position beneath the bars. > Is there any (easy?) solution? > > - how can I set the y-axis in a barplot to logarithmic scale? > > Many thanks in advance! > > Best Regards > TomThe same concept for rotating axis labels in a regular plot per the FAQ can be used in a barplot, as long as you know that barplot() returns the bar midpoints. So: ## Increase bottom margin to make room for rotated labels par(mar = c(7, 4, 4, 2) + 0.1) ## Create plot and get bar midpoints in 'mp' mp <- barplot(1:10) ## Set up x axis with tick marks alone axis(1, at = mp, labels = FALSE) ## Create some text labels labels <- paste("Label", 1:10, sep = " ") ## Plot x axis labels at mp text(mp, par("usr")[3] - 0.5, srt = 45, adj = 1, labels = labels, xpd = TRUE) ## Plot x axis label at line 4 mtext(1, text = "X Axis Label", line = 4) See ?barplot for more information. With respect to the log scales, you would need to use barplot2(), which is in the 'gplots' package on CRAN. barplot2() supports log scales using the same 'log' argument as plot(). HTH, Marc Schwartz
Try the following. I made this based on the eaxmple from FAQ 7.27 tab=table(rpois(100, 2)) #Creates table to make a barplot par(mar = c(6, 4, 4, 2) + 0.1)#Prepares margin to fit x axis labels pts=barplot(tab, xaxt = "n", xlab = "", col="yellow2")#Barplot of tab without x axis or label. #Also stores the middle points of the bars axis(side=1,at=pts, labels=F, tick=T)#Creates x axis with tickmarks exactly at the middle of the bars nam=paste("Text",names(tab))#Names of each category. To be used on labels text(pts, par("usr")[3] - 1.5, srt = 45, adj = 1, labels = nam, xpd = TRUE) #Adds the tickmark labels # adj = 1 will place the text at the end ot the tick marks # xpd = TRUE will "clip" text outside the plot region mtext(1, text = "My Axis Label", line = 4) #Adds x axis label at line 2 Second hit of RSiteSearch("logarithmic axis barplot") takes you to barplot2 {gplots} Cheers Francisco>From: hulubu at gmx.de >To: r-help at stat.math.ethz.ch >Subject: [R] How to rotate the axisnames in a BARPLOT >Date: Thu, 30 Jun 2005 23:05:15 +0200 (MEST) > >Hi all, > >- how can I do a barplot with rotated axis labels? I've seen the example >for >just a plot in the FAQ, but I'll missing the coordinates to plot my text at >the right position beneath the bars. >Is there any (easy?) solution? > >- how can I set the y-axis in a barplot to logarithmic scale? > >Many thanks in advance! > >Best Regards > Tom > >-- > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! >http://www.R-project.org/posting-guide.html