I'm having a problem with geom_bars wherein the bars are not rendered when I specify limits on the y-axis. I believe the following should reproduce the problem:
data <- structure(list(RoleCond = structure(c(1L, 1L, 2L, 2L), .Label = c("Buyer", "Seller"), class = "factor"), ArgCond = structure(c(1L, 2L, 1L, 2L), .Label = c("No Argument", "Argument"), class = "factor"), mean = c(2210.71428571429, 2142.70833333333, 2282.40740740741, 2346.2962962963), se = c(20.1231042081511, 16.7408757749718, 20.1471554637891, 15.708092540868)), .Names = c("RoleCond", "ArgCond", "mean", "se"), row.names = c(NA, -4L), class = "data.frame") library(ggplot2) ggplot(data=data, aes(fill=RoleCond, y=mean, x=ArgCond)) + geom_bar(position="dodge", stat="identity") + geom_errorbar(limits, position=dodge, width=0.1, size=.75) + scale_y_continuous(limits=c(2000,2500))
which gives me this
The same code without the limits specified works fine. The geom_errorbar() doesn't seem to be related to the problem, but it does illustrate where the bars should be showing up.
I've tried using coord_cartesian(ylim=c(2000,2500))
which works for limiting the yaxis and getting the bars to display, but the axis labels get messed up and I don't understand what I'm doing with it.
Thanks for any suggestions! (I'm using R 2.15.0 and ggplot2 0.9.0)
By default, geom_bar uses stat="bin". This makes the height of each bar equal to the number of cases in each group, and it is incompatible with mapping values to the y aesthetic. If you want the heights of the bars to represent values in the data, use stat="identity" and map a value to the y aesthetic."
To Increase or Decrease width of Bars of BarPlot, we simply assign one more width parameter to geom_bar() function. We can give values from 0.00 to 1.00 as per our requirements.
In the above example, we've overridden the default count value by specifying stat = "identity" . This indicates that R should use the y-value given in the ggplot() function. Notice that bar graphs use the fill argument instead of the color argument to color-code each cut category.
You could try, with library(scales)
:
+ scale_y_continuous(limits=c(2000,2500),oob = rescale_none)
instead, as outlined here.
Adding an answer for my case which was slightly different in case someone comes across this:
When using position="dodge"
, the bars get horizontally resized automatically to fill space that is often well beyond the limits of the data itself. As a result, even if both your x-axis
and y-axis
limits are limits=c(min-1, max+1
, for certain data sets, the position="dodge"
might resize it beyond that limit range, causing the bars to not appear. This might even occur if your limit floor is 0, unlike the case above.
Using oob=rescale_none
in both scale_y_continous()
AND scale_x_continuous()
fixes this issue by simply cutting off the resizing done by position="dodge"
.
As per earlier comment, it requires package:scales
so run library(scales)
first.
Hope this helps someone else where the above answers only get you part way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With