質問

I have to plot several IR-spectrums. The x-axis with this plots has to be stretched between 2000 and 500. I've tried axis(side=1,at=c(4000,3500,2000,1500,1000,500)), but this does not produce the same distance between the labels. I've searched nearly 2 hours but can't figure out how to achieve this.

Help would be appreciated.

Thanks in advance

役に立ちましたか?

解決

I don't think that there's a particularly clean way to do this in base graphics - no doubt there's something in one of the many graphics packages that would do it, but heres' my workaround for what I think you're trying to do.

#Some data to plot
x <- 0:4000
y <- sin(x/100)

#A function to do the stretching that you describe
stretcher <- function(x)
{
  lower <- 500       ##lower end of expansion
  upper <- 2000      ##upper end of expansion
  stretchfactor <- 3 ##must be greater than 1, factor of expansion
  x[x>upper] <- x[x>upper] + (stretchfactor-1) * (upper-lower)
  x[x<=upper & x>lower] <- (x[x<=upper & x>lower] - lower) * stretchfactor + lower
  x
}

#Create the plot
plot(stretcher(x),y,axes=FALSE)
labels <- c(4000,3500,3000,2500,2000,1500,1000,500)
box()
axis(2)
axis(1,labels=labels,at=stretcher(labels))

I'd also emphasis the breaks with something like:

abline(v=stretcher(2000),col='red',lty=2)
abline(v=stretcher(500),col='red',lty=2)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top