R:抑制轴端点的刻度和标签

时间:2016-07-25 14:59:43

标签: r 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

我希望在第1和第5位既不是刻度线也不是标签,但仍应绘制。这就是我要找的东西: The plot should look like this

使用labels = c("", 2, 3, 4, "")时会绘制刻度线。使用tick = FALSE时,我没有轴。有人有解决方案吗?

1 个答案:

答案 0 :(得分:2)

您只需手动绘制线条。使用this answer中的line2user功能:

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

注意,line2user函数只是给出了用户坐标系中线条的位置。您需要xpd = TRUE在绘图区域之外绘制。