Center x and y axis with ggplot2

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

Is there a way to get the axes, with labels in the center of a ggplot2 plot, like a traditional graphing calculator? I've looked through the docs and there doesn't seem to be that functionality, but other plotting packages are not as graphically customizable as ggplot2. To clarify, I was looking to go from something like this:

To this:

The first plot is made with the following code:

dat = data.frame(x = 1, y =1) p = ggplot(data = dat, aes(x=x, y=y)) + geom_point(size = 5) p + xlim(-2,2) + ylim(-2,2)

The second plot is made with Mathematica. The main problem I am having is figuring out how to make the axis, with labels, go to the center (I can make the theme blank, etc., no problem). There seems to be no theme parameter you can edit to make this a quick fix.

回答1:

I think this is what you are looking for:

I have constructed a function that does just that:

theme_geometry 

As an example you can run the following code to create an image similar to the one above:

simdata 


回答2:

I would just use xlim and ylim.

dat = data.frame(x = 1, y =1) p = ggplot(data = dat, aes(x=x, y=y)) +     geom_point(size = 5) +     xlim(-2, 2) +     ylim(-2, 2) p



回答3:

A first approximation:

dat = data.frame(x = 1, y =1) p = ggplot(data = dat, aes(x=x, y=y)) + theme_bw() +   geom_point(size = 5) +   geom_hline(aes(y = 0)) +   geom_vline(aes(x = 0))

Adjust limits as per SlowLearner's answer.



回答4:

There are some other useful answers, but the following comes closer to the target visual and avoids looping:

library(ggplot2) library(magrittr)  # constants axis_begin  %   subset(ticks != 0)  lab_frame %   subset(lab != 0)  tick_sz 


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