可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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