Plotting a new series on top of old

hlsmith

Less is more. Stay pure. Stay poor.
#1
Well, I don't quite have a solution in my brain or way to phrase this for a WWW search. I 'plot' a series (x = time, y = count) in base plot, then I run "par(new = TRUE)" and plot a new series (x=time, y = count/n) on top of the old and label the second y-axis accordingly to account for units of second series.

However for this new series it should start shifted over 22 integers on the x-axis. Its initial x-value is 23, but it is still starts above the day 1 value for the initial series. I haven't made a reproducible example since it seems like this could have a simple solution.

Code for series is:

Code:
plot(x = x_IS, y = y_IS, 
     type = "l", 
     lty = 2,
     lwd = 2, 
     col = "black", 
     axes = FALSE, 
     xlab = NA, 
     ylab = NA)
 

hlsmith

Less is more. Stay pure. Stay poor.
#2
This isn't my data, but I wonder if this works as an example:

Code:
x = c(5,5,5,5,5,5)
y = c(seq(1:6))
plot (y,x, type='l')
par(new = TRUE)
x2 = c(.6,.5,.4,.3)
y2 = c(3,4,5,6)
plot(y2,x2, type='l',
     axes = FALSE,
     xlab = NA,
     ylab = NA)
1587501692123.png

So the second series should start at 3, not 1.
 

hlsmith

Less is more. Stay pure. Stay poor.
#4
this thing is goddam np hard, every little hack doesn't quite work.

For example this doesn't work:

Code:
x = c(5,5,5,5,5,5)
y = c(seq(1:6))
plot (y,x, type='l')
par(new = TRUE)
x3 = c(.6,.5,.4,.3, .2, .1)
y3 = c(seq(1:6))
plot(y3,x3, type='l', 
     axes=FALSE, 
     xlab = NA, 
     ylab = NA,
     col='blue')
x2 = c(.6, .4, .4, .6)
y2 = c(3,4,5,6)
plot(y2,x2, type='l',
    xlab = NA, 
     ylab = NA,
     col='green')
 
Last edited:

trinker

ggplot2orBust
#6
I'm sorry hlsmith but it's still not clear to me what you want. Can you draw the image by hand, take a pic of what you'd expect it to do? Also why plot x on the y axis and y on the x axis Or just poor variable names?
 

hlsmith

Less is more. Stay pure. Stay poor.
#7
The issue is if you look at x2 variable below, it should be graphed starting at the 3 mark on the x-axis, but given the par(new=T) it starts on the far side of the x-axis, which appears to be the 1 value, but the variable is actually a 3 value. So it takes the new data and stretches it across the graph. I get why it is doing this, because of the par statement. However the par statement is needed since the other term is not in the same units as the original series y-variable. So I need the par to plot a new series with different units on one axis, but the units on the other axis need to be the same as the original. Thus using the following code the green line should start on the third tick of the x-axis.

Code:
y = c(5,5,5,5,5,5)
x = c(seq(1:6))
plot (x,y, type='l', col='blue')
par(new = TRUE)

y2 = c(.6, .5, .4, .3)
x2 = c(3,4,5,6)
plot(x2,y2, type='l',
      xlab = NA,
      ylab = NA,
      col='green',
      axes=FALSE)
axis(side = 4)
1587643703224.png
 

Dason

Ambassador to the humans
#8
Something like this maybe
Code:
y = c(5,5,5,5,5,5)
x = c(seq(1:6))
y2 = c(.6, .5, .4, .3)
x2 = c(3,4,5,6)

plot (x,y, type='l', col='blue')
par(new = TRUE)


# Decide what you want the plotting limits to be
# use the full range of x values
xr <- range(c(x, x2))
# but only the range for the y values
yr <- range(y2)

# plot the ranges you want x and y to go
# but type='n' keeps it from plotting the data
# this sets up the plot for you
plot(xr,yr, type='n',
      xlab = NA,
      ylab = NA,
      axes=FALSE)
# now plot your data
lines(x2, y2, col = 'green')
axis(side = 4)
 

hlsmith

Less is more. Stay pure. Stay poor.
#9
Cool, I will have to check this out, cause my current hack could have issues in the future, where I added and minused obs. The 'type='n'' is new to me, I had used 'col='white'' before.

Also, how isn't 'minused' a word???