user8060
user8060

Reputation: 63

R: How to strech the x axis values?

I have a very basic question. I have created a basic x,y-plot and for my x values which span from -1000 to 0 I would like to stretch last 50 units on x-axis so that from -1000 to -50 values are by default one unit apart but from -50 to 0 they should be 10 units apart. is this possible ??? and how would someone like me do this??

thank you

m

UPDATE: oh, sorry i thought .... well never mind, thnx for the tip:

example

x <- c(-1000:-1)
y <- c(1:1000)
plot(x,y)

Now the plot is simple linear but i would like to have last 50 x-ticks 10 units apart?? does this make sense??

Upvotes: 0

Views: 2909

Answers (2)

C8H10N4O2
C8H10N4O2

Reputation: 19005

How about just an x axis transformation:

x <- c(-1000:-1)
y <- c(1:1000)
plot(x,y, type='l')

stretch <- function(x){
  y <- x
  y[x >= -50] <- 10*x[x >= -50]
  y[x < -50] <- x[x < -50] - 500
  y
}

plot(stretch(x), y, type='l', axes=FALSE, frame.plot=TRUE, xlab="", ylab="")

Alternatively, you can use lattice, for starters:

df <- data.frame(x,y)
df$panel <- as.integer(df$x > -50)
library(lattice)
with(df, xyplot(y~x|panel, scales=list(x=list(relation="free"))))

but there is a lot of tweaking needed to get rid of the break between charts there.

Upvotes: 2

Sam Dickson
Sam Dickson

Reputation: 5239

One way is to transform x and re-label the x-axis:

xt <- x
xt[x < -50] <- x[x < -50] - 450
xt[x >= -50] <- 10*x[x >= -50]

plot(xt,y,xaxt="n")
axis(1,at=c(200*(-(5:1))-450,-500,-250,0),
     labels = c(-1000,-800,-600,-400,-200,-50,-25,0))

enter image description here

Upvotes: 2

Related Questions