如何控制ggplot的绘图区域比例而不是将它们安装到R中的设备上? [英] How to control ggplot's plotting area proportions instead of fitting them to devices in R?

查看:1222
本文介绍了如何控制ggplot的绘图区域比例而不是将它们安装到R中的设备上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下, ggplot 中的每个图都适合它的设备。



这并不总是可取的。例如,可能需要将 geom_tile 中的图块制成正方形。一旦你改变了设备或改变了x / y轴上的元素数量,瓷砖不再是正方形。

是否可以设置硬比例或尺寸一个情节,并将其绘制在其设备窗口中(或使设备的宽度和高度与绘图的宽度和高度成正比)?

解决方案

您可以使用coord_fixed()来指定图的纵横比。

 >库(ggplot2)
> df< - data.frame(
+ x = runif(100,0,5),
+ y = runif(100,0,5))

如果我们继续绘制这些数据,然后我们得到一个符合输出设备尺寸的图。

 > ggplot(df,aes(x = x,y = y))+ geom_point()

然而,我们使用coord_fixed(),然后我们得到一个固定纵横比的图(默认情况下x和y轴的长度相同)。该图的大小将由输出设备的最短尺寸决定。

 > ggplot(df,aes(x = x,y = y))+ geom_point()+ coord_fixed()

最后,我们可以通过指定coord_fixed()参数来调整固定高宽比,其中参数是y轴长度与x轴长度的比值。因此,为了得到一个高出它宽度两倍的阴谋,我们将使用:

 > ggplot(df,aes(x = x,y = y))+ geom_point()+ coord_fixed(2)


By default, each plot in ggplot fits its device.

That's not always desirable. For instance, one may need to make tiles in geom_tile to be squares. Once you change the device or change the number of elements on x/y-axis, the tiles are no longer squares.

Is it possible to set hard proportions or size for a plot and fit the plot in its device's window (or make the device width and height proportional to those of the plot)?

解决方案

You can specify the aspect ratio of your plots using coord_fixed().

> library(ggplot2)
> df <- data.frame(
+     x = runif(100, 0, 5),
+     y = runif(100, 0, 5))

If we just go ahead and plot these data then we get a plot which conforms to the dimensions of the output device.

> ggplot(df, aes(x=x, y=y)) + geom_point()

If, however, we use coord_fixed() then we get a plot with fixed aspect ratio (which, by default has x- and y-axes of same length). The size of the plot will be determined by the shortest dimension of the output device.

> ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed()

Finally, we can adjust the fixed aspect ratio by specifying an argument to coord_fixed(), where the argument is the ratio of the length of the y-axis to the length of the x-axis. So, to get a plot that is twice as tall as it is wide, we would use:

> ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed(2)

这篇关于如何控制ggplot的绘图区域比例而不是将它们安装到R中的设备上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆