1

I have a twoord plot produced with the plotrix package and would like to add a horizontal line representing a particular value to it. The plot is all set up but I need help adding the line.

Here is some sample code:

fake <- matrix(c(1, 2, 3, 4, 5, 22, 30, 47, 98, 62, 20, 40, 10, 15, 15), nrow = 5)
fake <- as.data.frame(m)

horizontallineat <- 50

twrd.p <- twoord.plot(fake$V1,fake$V3,fake$V1,fake$V2, xlab="Bin", 
                  lylim=c(0,100),rylim=c(0,100),type=c("bar","l"),
                  ylab="Exposure Percentage",rylab="Bin Average PP",
                  lytickpos=seq(0,100, by = 10),
                  rytickpos=seq(0,100, by = 10),
                  ylab.at=50,rylab.at=50,
                  main="Variable Name",
                  lcol=3,rcol=4)

This is the plot

enter image description here

Thank you in advance for any insight you are able to offer.

KoenV
  • 4,113
  • 2
  • 23
  • 38
heat763
  • 13
  • 3
  • 1
    @d.b Did you want `h=50` ? – G5W Jun 08 '17 at 16:53
  • Unfortunately it isn't as simple as abline... I have tried just about everything I can think of. The documentation says: "Note that more values can be added to the plot using ‘points’ or ‘lines’, but remember that these will be plotted relative to the left ordinate." but I haven't had any luck with lines. – heat763 Jun 08 '17 at 17:16

1 Answers1

0

I am not sure whether this is what you want, but you can simply add line with the R-base lines function. Use code like this

library(plotrix) # added library

## your code
fake <- matrix(c(1, 2, 3, 4, 5, 22, 30, 47, 98, 62, 20, 40, 10, 15, 15), nrow = 5)
fake <- as.data.frame(fake)  # changed "m" to "fake"

twrd.p <- twoord.plot(fake$V1,fake$V3,fake$V1,fake$V2, xlab="Bin", 
                      lylim=c(0,100),rylim=c(0,100),type=c("bar","l"),
                      ylab="Exposure Percentage",rylab="Bin Average PP",
                      lytickpos=seq(0,100, by = 10),
                      rytickpos=seq(0,100, by = 10),
                      ylab.at=50,rylab.at=50,
                      main="Variable Name",
                      lcol=3,rcol=4)

## simple lines() function with x an y coordinates
## we'll add 2 lines for fun
## 1. dashed, thicker, and red
## 2. dots, thicker and black
lines(x = c(1, 2, 3, 4), y= c(40, 60, 40, 70), lty = 2, lwd = 2, col = "red")
lines(x = c(1.25 , 4.75), y = c(95, 25), lty = 3, lwd=2, col = "black")

This yields the following plot

Your plot with 2 lines added

Adding a short or long horizontal line should be simple now, I hope.

Please let me know whether this is what you had in mind.

KoenV
  • 4,113
  • 2
  • 23
  • 38
  • This solution did work. Thank you. The only caveat is that it only plots relative to the left axis scale so I needed to swap them. – heat763 Jun 09 '17 at 17:14
  • Glad I could help! I do not understand your comment, though, because as far as I can see the left and right axis scale are the same. – KoenV Jun 09 '17 at 17:24
  • This was sample data... my actual data is not the same scale on the left and right axis. lines() will only plot relative to the left axis scale so I had to swap axes. – heat763 Jun 09 '17 at 21:32
  • Of course, I should have guessed that. I don't want to beg or pressure you, but this site works with voting: see **[why voting is important](https://stackoverflow.com/help/why-vote)**. Like I voted up your question, because I believe it is well formulated and deserves attention, you could signal that the answer helped you by "voting it up" or by marking it as the "solution to your problem". Enjoy StackOverflow :o) – KoenV Jun 10 '17 at 05:34
  • I attempted to vote you up but I'm afraid that my experience level will not allow me. My apologies. – heat763 Jun 12 '17 at 16:08