I'm trying to resize a plot to fit into my document, but I'm having difficulties getting the plotted diagram do be a square.
我试着调整一个图的大小以适应我的文档,但是我很难把这个图画成正方形。
Example:
例子:
pdf(file = "./out.pdf", width = 5, height = 5)
p <- ggplot(mydata, aes(x = col1, y = col2))
print(p)
aux <- dev.off()
Although the limits for x and y are the same, the plot in the result isn't square. I guess that R makes the enclosing panel 5x5" but doesn't care about the actual diagram size.
虽然x和y的极限是相同的,但是结果的图形并不平方。我猜是R制作了封装的面板5x5"但是并不关心实际的图大小。
How can I unsquash my diagrams?
我怎样才能解压缩我的图表?
3 个解决方案
#1
79
In ggplot
the mechanism to preserve the aspect ratio of your plot is to add a coord_fixed()
layer to the plot. This will preserve the aspect ratio of the plot itself, regardless of the shape of the actual bounding box.
在ggplot中,保存图的纵横比的机制是向图中添加coord_fixed()层。这将保持图形本身的纵横比,而不考虑实际边框的形状。
(I also suggest you use ggsave
to save your resulting plot to pdf/png/etc, rather than the pdf(); print(p); dev.off()
sequence.)
(我也建议你使用ggsave将你的结果保存为pdf/png/等格式,而不是pdf();打印(p);dev.off()序列。)
library(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()
#2
48
To ensure a particular aspect ratio, e.g. for square, use theme(aspect.ratio=1)
.
为了确保一个特定的纵横比,例如正方形,使用主题(aspect.ratio=1)。
Andrie's answer doesn't give the full picture, as the example provides perhaps unnatural data where range of x equals the range of y. If however the data were:
Andrie的回答并没有给出完整的结果,因为这个例子可能提供了不自然的数据,其中x的范围等于y的范围。
df <- data.frame(
x = runif(100, 0, 50),
y = runif(100, 0, 5))
ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed()
then the plot would look like this:
然后情节是这样的:
The coord_fixed() function also has an argument to adjust the ratio of axes:
coord_fixed()函数还有一个参数来调整坐标轴的比例:
ratio
aspect ratio, expressed as y / x比值纵横比,表示为y / x。
So that the plot could be made square with:
这样,我们就可以把整个情节平面化:
ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed(ratio=10)
But you need to adjust this with the limits of the variables or plot area (not all limits are nicely divisible by whole numbers like these examples).
但是您需要使用变量或绘图区域的限制来调整它(并不是所有的限制都可以很好地被像这些例子那样的整数整除)。
#3
6
For completeness sake: If you want to take very different axis limits into account:
为了完整性起见:如果您想考虑非常不同的轴的限制:
df <- data.frame(
x = runif(100, 0, 5000),
y = runif(100, 0, 5))
ratio.display <- 4/3
ratio.values <- (max(df$x)-min(df$x))/(max(df$y)-min(df$y))
plot <- ggplot(df, aes(x=x, y=y)) + geom_point()
plot + coord_fixed(ratio.values / ratio.display)
Resulting in:
导致:
#1
79
In ggplot
the mechanism to preserve the aspect ratio of your plot is to add a coord_fixed()
layer to the plot. This will preserve the aspect ratio of the plot itself, regardless of the shape of the actual bounding box.
在ggplot中,保存图的纵横比的机制是向图中添加coord_fixed()层。这将保持图形本身的纵横比,而不考虑实际边框的形状。
(I also suggest you use ggsave
to save your resulting plot to pdf/png/etc, rather than the pdf(); print(p); dev.off()
sequence.)
(我也建议你使用ggsave将你的结果保存为pdf/png/等格式,而不是pdf();打印(p);dev.off()序列。)
library(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()
#2
48
To ensure a particular aspect ratio, e.g. for square, use theme(aspect.ratio=1)
.
为了确保一个特定的纵横比,例如正方形,使用主题(aspect.ratio=1)。
Andrie's answer doesn't give the full picture, as the example provides perhaps unnatural data where range of x equals the range of y. If however the data were:
Andrie的回答并没有给出完整的结果,因为这个例子可能提供了不自然的数据,其中x的范围等于y的范围。
df <- data.frame(
x = runif(100, 0, 50),
y = runif(100, 0, 5))
ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed()
then the plot would look like this:
然后情节是这样的:
The coord_fixed() function also has an argument to adjust the ratio of axes:
coord_fixed()函数还有一个参数来调整坐标轴的比例:
ratio
aspect ratio, expressed as y / x比值纵横比,表示为y / x。
So that the plot could be made square with:
这样,我们就可以把整个情节平面化:
ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed(ratio=10)
But you need to adjust this with the limits of the variables or plot area (not all limits are nicely divisible by whole numbers like these examples).
但是您需要使用变量或绘图区域的限制来调整它(并不是所有的限制都可以很好地被像这些例子那样的整数整除)。
#3
6
For completeness sake: If you want to take very different axis limits into account:
为了完整性起见:如果您想考虑非常不同的轴的限制:
df <- data.frame(
x = runif(100, 0, 5000),
y = runif(100, 0, 5))
ratio.display <- 4/3
ratio.values <- (max(df$x)-min(df$x))/(max(df$y)-min(df$y))
plot <- ggplot(df, aes(x=x, y=y)) + geom_point()
plot + coord_fixed(ratio.values / ratio.display)
Resulting in:
导致: