geom_bar bars not displaying when specifying ylim

后端 未结 3 1944
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 05:15

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:

         


        
相关标签:
3条回答
  • 2020-11-29 05:44

    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.

    0 讨论(0)
  • 2020-11-29 05:53

    This worked for me based on the link shared previously.

    p + coord_cartesian(ylim=c(5,15))
    
    0 讨论(0)
  • 2020-11-29 05:54

    You could try, with library(scales):

    + scale_y_continuous(limits=c(2000,2500),oob = rescale_none)
    

    instead, as outlined here.

    0 讨论(0)
提交回复
热议问题