R语言 创建特定尺寸的绘图窗口
在这篇文章中,我们将讨论R编程语言中可用的各种方法来调整绘图窗口的大小。
方法1:使用dev.new()方法
可以指定绘图的宽度和高度,以及显示绘图的单位。这个方法的用法可以自定义,以便在绘图窗口的尺寸内很好地适应图形。
语法
dev.new(width, height, unit = c(in, px, cm))
例1:以英寸为单位指定尺寸
# declaring the xpos vector
xpos <- c(1:10)
# declaring the ypos vector equivalent
# to x^2
ypos <- xpos^2
# specifying the dimensions of the plot
# window in inches
dev.new(width=20, height=10, unit="in")
# plotting the points in plot window
plot(xpos,ypos, type="b")
输出
在这种情况下,高度和宽度的尺寸都比较大。
例2:以像素为单位指定尺寸
# declaring the xpos vector
xpos <- c(1:10)
# declaring the ypos vector equivalent
# to x^2
ypos <- xpos^8
# specifying the dimensions of the plot
# window in inches
dev.new(width=300, height=150, unit="px")
# plotting the points in plot window
plot(xpos,ypos, type="b")
输出
方法2:使用windows.options方法
windows.options()方法只在Windows机器上可用。宽度和高度参数可以指定给该方法以覆盖默认设置,并根据这些设置应用,直到使用factory.reset()刷新设置。
语法
windows.options(width, height, reset = FALSE)
例子
# declaring the xpos vector
xpos <- c(1:10)
# declaring the ypos vector equivalent
# to x^2
ypos <- xpos^2
# specifying the dimensions of the plot
# window in inches
windows.options(width = 20, height = 10, reset = FALSE)
# plotting the points in plot window
plot(xpos,ypos, type="b")
输出
方法3:使用noRStudioGD选项
noRStudioGD = TRUE是R中的一个可选参数,可用于在早期版本的R中绘制绘图。它可以在dev.new()方法中设置,以防该方法无法打开设备驱动程序。
语法
dev.new(width, height, unit = c(in, px, cm),noRStudioGD = TRUE)
例子
# declaring the xpos vector
xpos <- c(1:10)
# declaring the ypos vector equivalent
# to x^2
ypos <- xpos^1/4
# specifying the dimensions of the plot
# window in inches
dev.new(width=50, height=25, unit="cm",noRStudioGD = TRUE)
# plotting the points in plot window
plot(xpos,ypos, type="b")
输出