0
library(tidyverse)
df <- tibble(col1 = c("A", "B"), col2 = c(0.4, 0.7))
#> # A tibble: 2 x 2
#>   col1   col2
#>   <chr> <dbl>
#> 1 A       0.4
#> 2 B       0.7

ggplot(df, aes(col1, col2)) + geom_col()

A ggplot of the data frame above looks like this, with breaks that include decimal numbers.

integer 1

There are multiple methods for specifying integer breaks from this stackoverflow question. None of them seem to do what I want. I want there to be two breaks, one at 0 and the other at 1. How do I modify one of these functions below to accomplish that?

# Attempt 1 
ggplot(df, aes(col1, col2)) + 
  geom_col() + 
  scale_y_continuous(
    breaks = function(x) unique(floor(pretty(seq(0, (max(x) + 1) * 1.1)))))

# Attempt 2
ggplot(df, aes(col1, col2)) + 
  geom_col() + 
  scale_y_continuous(breaks = scales::pretty_breaks(2))

# Attempt 3
ggplot(df, aes(col1, col2)) + 
  geom_col() + 
  scale_y_continuous(breaks = c(0, 1))

# Attempt 4
ggplot(df, aes(col1, col2)) + 
  geom_col() + 
  scale_y_continuous(
    breaks = function(x) seq(ceiling(x[1]), floor(x[2]), by = 1))

# Attempt 5
ggplot(df, aes(col1, col2)) + 
  geom_col() + 
  scale_y_continuous(
    breaks = 
      function(x, n = 5)  pretty(x, n)[round(pretty(x, n),1) %% 1 == 0])

Most of the attempts above produce the plot below. None produce what I want. Notice the break for 1 is missing.

integer 2

Display name
  • 4,153
  • 5
  • 27
  • 75
  • 1
    Try adding `limits = c(0,1)` to your scale_y_continuous. It is probably because your max value is 0.7, the breaks argument does nothing to the scale of the axis, just the breaks. So you do have the breaks 0 and 1, 1 is just outside the range of the axis – NotThatKindODr May 20 '20 at 13:26
  • 2
    ...making it `scale_y_continuous(breaks = c(0, 1), limits = c(0, 1))` – Gregor Thomas May 20 '20 at 13:29
  • @Gregor Thomas I'm trying to go with a programmatic solution if possible, because I won't have the ability to manually change the breaks and limits for each data frame this will get run on. Some will need breaks at `0` and `1`, many will not. – Display name May 20 '20 at 13:46
  • 1
    It seems like there are two issues then - (a) making your `breaks` only use integers, and (b) adjusting the limits so that... could use some more specificity here. In your example, you got 1 integer break at 0, but that's not good. Do you always want the scale to include at least 2 breaks? Always start at 0? Anything else? – Gregor Thomas May 20 '20 at 13:58
  • this post can demonstrate what I need in a better fashion https://stackoverflow.com/questions/61915427/ggplot-integer-breaks-on-facets – Display name May 20 '20 at 15:13

1 Answers1

3

That's because your y-axis is not going far enough to see the break at 1, it's actually there, you just don't see it :) Simply adjusting the y-axis limits should fix it.

Here's a simple solution:

ggplot(df, aes(col1, col2)) + 
  geom_col() + 
  scale_y_continuous(breaks = c(0, 1), limits = c(0, 1))

enter image description here

milanmlft
  • 411
  • 3
  • 14
  • This works but I'm trying to avoid manual solutions. Can this be done modifying the functions in my attempts? I need it to work on a wide variety of data frames (with limits other than 0 and 1), not just this specific one (but would need to work on this one as well). – Display name May 20 '20 at 13:47
  • 1
    But so in each case you want just 2 breaks? And if the values are 3 and 9, for example (like you wrote in your edited post), where should the breaks be? You'll need some general rule to modify the limits in each case I guess. Note that it's also possible to provide a function to `limits = ` to adapt the existing ones (see `?scale_y_continuous` for more details) – milanmlft May 20 '20 at 13:59
  • new post is here hopefully is clearer https://stackoverflow.com/questions/61915427/ggplot-integer-breaks-on-facets – Display name May 20 '20 at 15:13