1

I have the following dataset:

x = c(4, 5, 10, 30, 50, 51)
y = sqrt(x)

And I'd like to plot the data in the x-range from 5-50

plot(x,y, xlim=c(5, 50))
abline(v=c(5,50), col="red")

But still datapoints from outside of that range are visible. Is there an easy way to tell the plot() to tkae the limit literally. So far I came up with two ideas:

  1. Remove margins of the plot (I actually like the margins, but not the data that become visible)
  2. Exclude the data from the variables before plotting them (in my experience that was always tedious - subset() might work when you organize your data in a data.frame)
4

1 回答 1

1

默认情况下,R 在尝试找到漂亮的标签中断(例如2,4,65,10,15等)之前会稍微扩展轴的范围(4%)。par您可以使用参数来覆盖它xaxs="i"- 代表internal,您可以将其作为...参数之一传递给 plot 调用:

plot(x,y, xlim=c(5, 50) , xaxs = "i" )

在此处输入图像描述

帮助页面par中可能有用的一些信息:

xaxs
用于 x 轴的轴间隔计算样式。可能的值是“r”、“i”、“e”、“s”、“d”。
样式通常由数据范围或 xlim(如果给定)控制。

  • 样式“r”(常规)首先在每一端将数据范围扩展 4%,然后找到一个带有漂亮标签且适合扩展范围的轴。
  • 样式“i”(内部)只是找到一个带有漂亮标签的轴,该标签适合原始数据范围。
于 2013-09-09T09:28:34.390 回答