Force the origin to start at 0

190,808

Solution 1

xlim and ylim don't cut it here. You need to use expand_limits, scale_x_continuous, and scale_y_continuous. Try:

df <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(df, aes(x, y)) + geom_point()
p <- p + expand_limits(x = 0, y = 0)
p # not what you are looking for

enter image description here

p + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0))

enter image description here

You may need to adjust things a little to make sure points are not getting cut off (see, for example, the point at x = 5 and y = 5.

Solution 2

Simply add these to your ggplot:

+ scale_x_continuous(expand = c(0, 0), limits = c(0, NA)) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, NA))

Example

df <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(df, aes(x, y)) + geom_point()
p <- p + expand_limits(x = 0, y = 0)
p # not what you are looking for


p + scale_x_continuous(expand = c(0, 0), limits = c(0,NA)) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, NA))

enter image description here

Lastly, take great care not to unintentionally exclude data off your chart. For example, a position = 'dodge' could cause a bar to get left off the chart entirely (e.g. if its value is zero and you start the axis at zero), so you may not see it and may not even know it's there. I recommend plotting data in full first, inspect, then use the above tip to improve the plot's aesthetics.

Solution 3

In the latest version of ggplot2, this can be more easy.

p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point()
p+ geom_point() + scale_x_continuous(expand = expansion(mult = c(0, 0))) + scale_y_continuous(expand = expansion(mult = c(0, 0)))

enter image description here

See ?expansion() for more details.

Share:
190,808

Related videos on Youtube

Jonas Stein
Author by

Jonas Stein

Updated on September 02, 2020

Comments

  • Jonas Stein
    Jonas Stein over 3 years

    How can I set the origin / interception of the y-axis and x-axis in ggplot2?

    The line of the x-axis should be exactly at y=Z.

    With Z=0 or another given value.

  • JelenaČuklina
    JelenaČuklina about 8 years
    I also needed to specify limits: scale_x_continuous(expand = c(0, 0), limits = c(0,5)), somehow without it it didn't work
  • Michael Roswell
    Michael Roswell over 4 years
    I think one more piece can be helpful, which is using something like expand=expand_scale(mult=c(0,0.1)) so you still get the padding at the upper ends: stackoverflow.com/a/59056123/8400969
  • Bolle
    Bolle almost 4 years
    is it also possible to build this into a new ggplot theme?
  • stevec
    stevec almost 4 years
    @Bolle I’m not sure, but interested to find out as well, you could ask as a separate question and link to here
  • stevec
    stevec almost 4 years
    Link here for future reference
  • Melkor.cz
    Melkor.cz over 3 years
    This only changes padding around data points, but does not help set axes origin to zero or other desired value.