[英]Two scales in the same axis
I'm not sure if it's possible do what I want to. 我不确定是否有可能做我想做的事。 I want to draw an x axis with two scales like the picture below.
我想用两个比例绘制一个x轴,如下图所示。 Thanks a lot,
非常感谢,
ggplot2
version can look like this: ggplot2
版本看起来像这样:
library(ggplot2)
x = c(1,2,3,4,5, 10,20,30,40,50)
y = c(1,2,2,3,4, 2,1,3,5,5)
# You should introduce cond - condition to separate axises - by yourself
df = data.frame(x=x,y=y,cond=ifelse(x>5,"x2","x1"))
ggplot(df, aes(x,y,group=cond)) + geom_line() + geom_point(aes(shape=cond), size=4) + facet_grid(.~cond, scales="free_x")
Which produces this plot: 这产生了这个情节:
Here is a version using base R graphics. 这是一个使用基础R图形的版本。 You can probably play with it a bit more to get things just how you want it, but this is basically what you are after.
您可以更多地使用它来获得您想要的东西,但这基本上就是您所追求的。
plot.new()
par(mfcol=c(1,2))
plot(1:5,ann=FALSE,bty="n",type="l",ylim=c(0,25))
grid(ny=NA)
par(mar=c(5.1, 0, 4.1, 2.1))
plot(c(10,20,30,40,50),c(8,5,15,20,20),yaxt="n",ann=FALSE,bty="n",type="l",ylim=c(0,25))
grid(ny=NA)
par(new=TRUE)
par(mfcol=c(1,1))
par(bty="l")
par(mar=c(5.1, 4.1, 4.1, 2.1))
plot(NA,ylim=c(0,25),type="n",xaxt="n",yaxt="n",ann=FALSE)
box()
grid(nx=NA,ny=NULL)
edit- snazzed it up a bit 编辑 - 把它搞砸了一下
I'm not altogether sure, but as a starting point, take a look at ?facet_grid()
in ggplot2
. 我并不完全确定,但作为一个起点,看一看
?facet_grid()
在ggplot2
。
Getting the two plots side by side should not be too hard. 并排获得两个地块不应该太难。 Then you would probably want to turn off the axis and then add back the appropriate scales
然后你可能想要关闭轴然后添加适当的比例
Ask google about axis.line = theme_blank()
问谷歌关于
axis.line = theme_blank()
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.