1

I am trying to produce a plot where my continuous scale starts at 1 rather than 0. When I change limits in scale_y_continuous to limits=c(1, 7) the following message comes up:

Removed 7 rows containing missing values (geom_bar).

library(ggplot2)
fig4 <- ggplot(AccessToCare, aes(x = reorder(itemShort, mean), y = mean)) 
+ geom_bar(stat = "identity", width = 0.5) 
+ scale_x_discrete(name=" ") 
+ scale_y_continuous(name="Rating", breaks=seq(0,7,1), limits=c(0, 7)) 
+ geom_text(aes(x = itemShort, y = mean + 0.5, label = round(mean, 2)), size = 5) 
+ theme_classic() 
+ theme(text=element_text(size=20)) 
+ coord_flip() 

I'm certain that there are no missing values in the df.

3 Answers3

0

Welcome to SO ! The error message means that you have some values that are below 0 or above 7 and consequently they were filtered it out by ggplot when building the plot.

It is hard to be sure what are the consequences of this on your graph because you did not report any data we can try or an image of your graph. If it is an issue you can try to use coord_cartesian(ylim = c(0,7)) in your code and remove limits from scale_y_continuous.

However, to reply to your question and get your scale starting at 1 instead of 0, you need to change scale_y_continuous by this:

scale_y_continuous(name="Rating", breaks=1:7, limits=c(0, 7)) 

Does it answer your question ?

If not, please provide a reproducible exmaple of your dataset by following this guide: How to make a great R reproducible example

dc37
  • 15,840
  • 4
  • 15
  • 32
  • Thank you for the warm welcome, @dc37! Thank you for spotting a mistake, I meant that I attempted changing to `limits=c(1:7)`. It was my goal to have both the scale and the y-axis to start at 1. However, your comment taught me how skip visualising the 0 which I will definitely use in the future, thank you! `coord_cartesian(ylim = c(0,7))` solution did not work for some reason. No error was given, just the graph did not change visually (I removed `limits` as you suggested.) I accidentally found a solution though, please see my answer! Curious what you think. – kaleidoscopic Apr 24 '20 at 09:03
0

By default, ggplot2 removes any values beyond the scale limits. The argument in the scales that controls what happens to the out of bounds values is the oob argument.

Because the bars in your barplot are internally parameterised as rectangles via xmin/xmax and ymin/ymax, it considers the xmin part out of bounds when you set regular scale limits above 0.

I've answered a very similar question here, but here is the tl;dr.

First we make a plot with some dummy data

library(ggplot2)

df <- data.frame(x = LETTERS[1:7],
                 y = 1:7)

g <- ggplot(df, aes(x, y)) +
  geom_col() # friendly reminder that this is shorthand for geom_bar(stat = "identity")

This is the effect of squishing out-of-bounds values, they are set to the nearest limit.

g + scale_y_continuous(limits = c(1, 7), 
                       oob = scales::squish)

If you'd like to keep the data as is, you can use a function that returns it's input unchanged. I was allowed to add this to the dev scales library.

g + scale_y_continuous(limits = c(1, 7),
                       oob = function(x, ...){x})

You can affect the limits in the same way as above by using the coordinate limits.

g + coord_cartesian(ylim = c(1, 7))

Created on 2020-04-23 by the reprex package (v0.3.0)

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Thank you for the detailed answer, @teunbrand! I did not know about the behind the scenes parameterisation of xmin/xmax. – kaleidoscopic Apr 24 '20 at 09:06
0

I believe I accidentally found a simple solution to my query and wanted to share.

I added expand to scale_y_continuous:

... + scale_y_continuous(name="Rating", breaks=1:7, expand = c(0, -1), limits=c(0,8)) + ...

This led to:

  • The y-axis to start at 1 and end at 7.
  • The scale labels to start at 1 and end at 7.

New Graph

I had not seen elsewhere suggest to use negative values for expand so I thought it would be valuable to share.

teunbrand
  • 33,645
  • 4
  • 37
  • 63