Rafael
Rafael

Reputation: 617

How can I draw an x axis with two scales?

I want to draw an x axis with two scales like the picture below:

enter image description here

Upvotes: 5

Views: 361

Answers (3)

Ricardo Saporta
Ricardo Saporta

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

thelatemail
thelatemail

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)

enter image description here

edit- snazzed it up a bit

Upvotes: 7

redmode
redmode

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: enter image description here

Upvotes: 9

Related Questions