-1

I tried to change y-axis scale range.
I used the code to set the y scale:

coord_cartesian(ylim = c(min(value) - 0.05, max(value) + 0.05))

where value is a numeric column.
I want the y-axis showing from the minimum of value minus 0.05 to maximum of value plus 0.05 with the breaks of 0.05.
However, coord_cartesian() does not work. Alternatively, I tried scale_y_continuous(breaks(min(value) - 0.05, max(value) + 0.05, 0.05)), it does not work either.

How to edit my code?

enter image description here

Peter Chen
  • 1,464
  • 3
  • 21
  • 48

3 Answers3

2

EDIT: original approach used coord_cartesian, but to specify breaks will need scale_y_continuous.

# Making fake data with similar range
mtcars$wt = mtcars$wt/6 + 0.7

ggplot(mtcars, aes(mpg, wt)) + 
  geom_point() +
  scale_y_continuous(breaks = 0.05*0:1000,
                     expand = expand_scale(add = 0.06)) # adjust to taste

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0

you can use ylim()

library(ggplot2)
data(mtcars)

ggplot(data = mtcars,
       aes(x = hp,
           y = mpg,
           color = cyl
           )
       ) +
  geom_point() +
  ylim(min(mtcars$mpg) - 0.05, max(mtcars$mpg) + 0.05) 

EDIT: I forgot to incorporate the max(value) + 1 component, which is now included

Graeme Frost
  • 2,438
  • 2
  • 13
  • 15
0

Try this:

+ scale_y_continuous(breaks = seq(min(value) - 0.05, max(value) + 0.05, by = 0.05))
caszboy
  • 48
  • 1
  • 8