简体   繁体   中英

How to crop plot area to the exact range of your data in R?

When I set limits to a plot in R, there is some distance between the limits I set and the actual limits of the plot area:

plot(c(1, 4), c(1, 4), ylim = c(1, 4))

在此处输入图片说明

Of course I can set the limits inside the outermost ticks so they appear to fall close to the edge of the plot area. I can get pretty close by drawing the axis separately and allowing it to be drawn outside the plot area:

plot(c(1, 4), c(1, 4), ylim = c(1.2, 3.8), axes = FALSE)
par(xpd = TRUE)
abline(h = 4, col = "grey")  # just to show the edge of the box
axis(2, at=c(1, 4), labels = c(1, 4))

在此处输入图片说明

But that's just eyeballing it. So:

How can I get the outermost ticks to fall exactly on the border of the plot area?

plot语句中的xaxs = 'i'yaxs = 'i'将使它分别分别精确地拟合x和y轴的数据( 详细信息 )。

绘图参数xaxsyaxs帮助您:

plot(c(1, 4), c(1, 4), xaxs="i")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM