Reputation: 617
I want to draw an x axis with two scales like the picture below:
Upvotes: 5
Views: 361
Reputation: 55360
I'm not altogether sure, but as a starting point, take a look at ?facet_grid()
in 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()
Upvotes: 1
Reputation: 93833
Here is a version using base R graphics. 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
Upvotes: 7
Reputation: 4941
ggplot2
version can look like this:
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:
Upvotes: 9