-1

With the ablineclip I can draw a straight line to any plot like:

 ablineclip(h = 2, x1 = 0,x2 = 5,lty = 2, col = "green")

this draws a horizontal line where y=2 from x=0 to x=5.

How can I draw a line for a second y axis?? (meaning a horizontal line where y value on the right axis is 2).

I have no code yet, sorry for the non reproductive code.

geo_dd
  • 283
  • 1
  • 5
  • 22
  • 1
    Which packages did you load? `ablineclip` is not included in the base package. – snaut Jan 17 '17 at 08:35
  • What have you searched / tried ? (Just to avoid suggesting the same things) – llrs Jan 17 '17 at 08:35
  • I am sorry, package 'plotrix'. @Llopis, I found only the 'triax.abline' which displays lines for triangle plots. – geo_dd Jan 17 '17 at 08:38
  • 1
    See [this blog post](https://www.r-bloggers.com/r-single-plot-with-two-different-y-axes/) Or this other question in SO http://stackoverflow.com/questions/6142944/how-can-i-plot-with-2-different-y-axes this might help you if I understood correctly – llrs Jan 17 '17 at 08:50
  • @Llopis thank you for the links, but I already have a plot with 2axis and I want to draw a horizontal line where y on the right axis is 2. This is an example with a single y axis and the 'abline'. https://rdrr.io/cran/plotrix/man/ablineclip.html – geo_dd Jan 17 '17 at 08:54

1 Answers1

2

Something like the following will work:

library(plotrix)
plot(1, type="n", xlim=c(-10,10), ylim=c(0,4))
ablineclip(h = 2, x1 = 0,x2 = 5,lty = 2, col = "green")
par(new=TRUE)
plot(1, type="n", xlim=c(-10,10), ylim=c(10,15), xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
ablineclip(h = 12, x1 = -4,x2 = 4,lty = 2, col = "red")

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
  • Thanks for the suggestion. With the 'par' works! Just wondering, is it possible to use the abline for the same thing with twoord plots? – geo_dd Jan 17 '17 at 09:05