Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How plot bars on top of grid lines when using barplot?

I am using grid() inside of barplot(). Because I want the bars to be plotted on top of the grid, I use the panel.first argument. However, the grid lines do not match the ticks of y-axis. Can anyone tell me how to solve this problem? My code is like below, thanks.

WRE <- c(1423, 41721)

bp <- barplot(WRE, xaxt = 'n', xlab = '', yaxt = 'n', las = 3, width = 0.5,
              space = 1.5, main = paste("How You Compare", sep = ""), ylab = "",  
              names.arg = NA, col = c("red","blue"), cex.lab = 1, 
              panel.first = grid(nx = NA, ny = NULL))

axis(1, at=bp, labels=c("Average Claims", "Your Claims"),
     tick=FALSE, las=1, line=-1, cex.axis=1) 
axis(2, at=axTicks(2), sprintf("$%s", formatC(axTicks(2), digits=9, big.mark=",", width=-1)),
     cex.axis=0.9, las=2)
like image 398
Carter Avatar asked Jan 27 '15 12:01

Carter


People also ask

Should plots have gridlines?

It depends on the points you would like to make with the graph. If you're just going to show an upward or downward trend, then the grid lines are probably redundant. If you'd need refer back to a certain point of a curve, and knowing the vertical position of that point would be crucial, then grid lines can help.

Which command is correct to add gridlines in the graph?

Click the chart, and then click the Chart Design tab. Click Add Chart Element > Gridlines. Choose the axis that you want to apply the gridlines to or click More Gridline Options to open the Format Major Gridlines pane.

What are grid lines on graphs?

Grid lines are lines that cross the chart plot to show axis divisions. Grid lines help viewers of the chart see what value is represented by an unlabeled data point. Especially for large or complicated charts, grid lines give valuable cues to the viewer. Grid lines come in two types: major and minor.


1 Answers

I think this might fall inside:
"panel.first: an ‘expression’ to be evaluated after the plot axes are set up but before any plotting takes place. This can be useful for drawing background grids or scatterplot smooths. Note that this works by lazy evaluation: passing this argument from other plot methods may well not work since it may be evaluated too early"

So, probably, the best way to do it is draw the barplot in 3 steps:

# Draw the barplot
bp <- barplot(WRE, xaxt = 'n',xlab = '',yaxt = 'n',las = 3, width =0.5,space = 1.5, main=paste("How You Compare", sep=""), ylab="",  names.arg=NA,  col=c("red","blue"), cex.lab=1)
# add the grid
grid(nx=NA, ny=NULL)
# redraw the bars...
barplot(WRE, xaxt = 'n',xlab = '',yaxt = 'n',las = 3, width =0.5,space = 1.5, ylab="",  names.arg=NA,  col=c("red","blue"), cex.lab=1, add=T)
# Then you can add the axes
axis(1, at=bp, labels=c("Average Claims", "Your Claims"), tick=FALSE, las=1, line=-1, cex.axis=1) 
axis(2, at=axTicks(2), sprintf("$%s", formatC(axTicks(2), digits=9, big.mark=",", width=-1)), cex.axis=0.9, las=2)
like image 129
Cath Avatar answered Oct 14 '22 01:10

Cath