Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)
edited by

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.

1 Answer

0 votes
by
edited by

To force the origin to start at zero, you can use the exapnd_limits function with scale_x_continuous, and scale_y_continuous as follows:

library("ggplot2")

df <- data.frame(x = 1:4, y = 1:4)

p <- ggplot(df, aes(x, y)) + geom_point()

p <- p + expand_limits(x = 0, y = 0) +theme_bw()

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

Output:

image

Related questions

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...