0

I have code producing the below pasted plot

x <- c(2, 3, 4)
y <- c(2.5, 4.1, 5.5)

plot(x, y, type = "o", xlim = c(1, 5), ylim = c(2, 6), axes = FALSE, bty = "n")
axis(side = 1, at = seq(1, 5, 1))
axis(side = 2, at = seq(2, 6, 1), las = 2)

The plot currently looks like this

I would like to have neither ticks nor labels at position 1 and 5, but the axis should still be drawn. This is what I am looking for: The plot should look like this

When using labels = c("", 2, 3, 4, "") ticks are drawn. When using tick = FALSE, I get no axis. Does anybody have a solution for this?

piptoma
  • 754
  • 1
  • 8
  • 19

1 Answers1

2

You just need to draw the line manually. Using the line2user function in this answer:

x <- c(2, 3, 4)
y <- c(2.5, 4.1, 5.5)

plot(x, y, type = "o", xlim = c(1, 5), ylim = c(2, 6), axes = FALSE, bty = "n")
axis(side = 1, at = 2:4)

lines(x = c(1, 5), y = rep(line2user(0, side = 1), 2), xpd = TRUE)
axis(side = 2, at = seq(2, 6, 1), las = 2)

enter image description here

Note, the line2user function just gives the position of the lines in the user coordinate system. You need the xpd = TRUE to draw outside the plotting region.

Community
  • 1
  • 1
dayne
  • 7,504
  • 6
  • 38
  • 56
  • Worked for me, after changing your code to `lines(x = c(1, 2), y = rep(line2user(0, side = 1), 2), xpd = TRUE)` and `lines(x = c(4, 5), y = rep(line2user(0, side = 1), 2), xpd = TRUE)`. Otherwise the axes got plotted twice (looked ugly on my screen:). – piptoma Jul 25 '16 at 15:13
  • 1
    @pipomas That is really unnecessary -- you can just as easily draw one line that goes from 1 to 5. They should overlap exactly. But, I'm glad it worked for you. – dayne Jul 25 '16 at 15:15
  • You are right. The axis is looking fine when exporting the plot as image or pdf. – piptoma Jul 25 '16 at 15:17