Force the origin to start at 0

筅森魡賤 提交于 2019-12-12 05:45:16

问题


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:


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

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

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.




回答2:


I understand from Hadley Wickham's answer in the link here that it is not possible in ggplot2 to have the axes in the middle of your graph. Someone in that url proposed to remove the standard axes and build them yourself.



来源:https://stackoverflow.com/questions/34352281/how-to-add-a-vertical-line-using-theme-function-in-my-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!