R plot, x-axis and y-axis touching

11,440

Solution 1

Most likely setting xaxs = "i" and yaxs = "i" will help you getting the desired behaviour.

plot(c(1,2,3),c(2,4,6),axes=F,xaxs = "i",yaxs="i",xlim=c(0,3),ylim=c(0,6))
axis(side=1, lwd=3, xpd=TRUE, at=0:3)
axis(side=2, lwd=3, xpd=TRUE, at=seq(0,6,2))

Solution 2

Try box(bty='L') to draw only the left and bottom parts of the box. You could also just draw the lines yourself using lines, segments, or abline and using grconvertX and grconvertY functions to find the locations where to draw the lines.

Solution 3

I suggest that you follow the procedure you outlined and then use:

box(which = "plot", bty = "l")

e.g.:

plot.new()
plot.window(xlim = c(1, 18), ylim = c(2, 20))
points(1:18, 2:19, pch = 1, col = "#FF7F24", cex = 1.2)
lines(1:18,  2:19, col = "#FF7F24", lwd = 2)
axis(side      = 1,
     lwd       = 0,
     lwd.ticks = 1,
     at        = 1:18,
     cex.axis = 0.9)
title(main = "Plot",
      ylab = "Y-Axis")
legend("top",
       legend = c("Legend"),
       col = c("#FF7F24"),
       text.col = c("#FF7F24"),
       pch    = 1,
       bty    = "n",
       cex    = 1.2)
axis(side      = 2,
     lwd       = 0,
     lwd.ticks = 1)
box(which = "plot", bty = "l")

You should pass the options lwd = 0 and lwd.ticks = 1 to your seperate axis() calls in order to prevent some parts of your axes to appear fatter than other parts of your axis because some get overlayed by your call to box() and some do not.

The solution of using box() at the end is, I think, more general in that you can use it when e.g. you cannot or do not want to pass bty = "l" in your plot.default or plot.window call.

Share:
11,440
Marnix de Zeeuw
Author by

Marnix de Zeeuw

Updated on June 04, 2022

Comments

  • Marnix de Zeeuw
    Marnix de Zeeuw almost 2 years

    My problem concerns the making of a graph for a publication in R. I have used the plot function like follows:

    plot(x=data$SL, y=data$BD, xlab = "SL (mm)", ylab = "BD (mm)", pch=data$pch)
    

    SL ranges from 51.7 to 73.7 and BD from 13.5 to 20.4. Unfortunately I am not allowed to post images yet.

    However, wanting to get rid of the box I used "axes=F". Problem now is lack of control over the axis function. I used:

    axis(side=1, lwd=3, xpd=TRUE, at=c(min(data$SL):max(data$SL)))
    axis(side=2, lwd=3, xpd=TRUE, at=c(min(data$BD):max(data$BD)))
    

    Problem is that I can't manage to get the y- and x-axis to come together on the same point as in the plot with the box. How to let the x- and y- axis to touch each other?