MotaF MotaF - 1 month ago 7
R Question

R plot,why is introducing of axis limits creating havoc?

My code

library(Hmisc)

r1 <- read.table("mt7.1r1.rp", header = FALSE)
r2 <- read.table("mt7.1r2.rp", header = FALSE)
r3 <- read.table("mt7.2r1.rp", header = FALSE)
r4 <- read.table("mt7.2r2.rp", header = FALSE)

p1=r1[1]
per1=log10(p1)
p2=r2[1]
per2=log10(p2)
p3=r3[1]
per3=log10(p3)
p4=r4[1]
per4=log10(p4)

m1=nrow(per1)
m2=nrow(per2)
m3=nrow(per3)
m4=nrow(per4)

xmin <- floor( min(per1,per2,per3,per4))
xmax <- ceiling( max(per1,per2,per3,per4))

lxmax=10^(xmax)
lxmin=10^(xmin)

rhoaxy = r2[3]
phaxy = r2[5]

rhoayx = r3[3]
phayx = r3[5]

rhoaxx = r1[3]
phaxx = r1[5]

rhoayy = r4[3]
phayy = r4[5]


per2=unname(per2)
per2=unlist(per2)
per3=unname(per3)
per3=unlist(per3)

rhoaxy=unname(rhoaxy)
rhoaxy=unlist(rhoaxy)
rhoaxy=log10(rhoaxy)
rhoayx=unname(rhoayx)
rhoayx=unlist(rhoayx)
rhoayx=log10(rhoayx)

ymin1=floor(min(rhoaxy)-1)
ymax1=ceiling(max(rhoaxy)+1)
ymin2=floor(min(rhoayx)-1)
ymax2=ceiling(max(rhoayx)+1)
ymin=min(ymin1,ymin2)
ymax=max(ymax1,ymax2)

png("withlim.png")

plot(per2,rhoaxy, col='red', xlab='Per (s)', ylab = 'Rho-xy/yx',ylim=c(ymin, ymax))
par(new=TRUE)
plot(per3,rhoayx, col='green', xaxt='n', xlab= NA, yaxt = 'n', ylab = NA)

dev.off()


The image I got
enter image description here

If I delete ylim
enter image description here

My question is,why are the axis limits changing the image content?The values from the second image correspond to proper data values.The first image is with values that do not represent rhoaxy and rhoayx.

Answer

It is difficult to test without the data, but my guess is that, on the second plot, the Y axis is not the same, although the Y axis is not plot.

So you've got the superposition of 2 plot, with a different Y axis. If you want the same ylim on both plot, add ylim=c(ymin, ymax) on the second plot also.

If it does not work, please provide data example, so we can test.

Comments