I am hitting a small, but not insignificant brick wall with this oft asked and answered question.
我正在问这个经常被问及回答问题的小砖墙,但并不是微不足道的砖墙。
I am using Rstudio 0.97.336 and R 3.0.0 on Linux. I am making a (much more complex) graph to put in a paper. The default size of the title and x/y labels are too small to be easily read. However the obvious method for fixing this using the theme function on element_text theme(axis.title.y = element_text(size = rel(1.8)) does not work, if I save the image as a PNG file. It does however work, exactly as expected, when I'm looking at the images in RStudio. The code below reproduces my problem exactly.
我在Linux上使用Rstudio 0.97.336和R 3.0.0。我正在制作一个(更复杂的)图表来填写论文。标题和x / y标签的默认大小太小,无法轻松阅读。然而,如果我将图像保存为PNG文件,那么使用element_text主题上的主题函数(axis.title.y = element_text(size = rel(1.8))修复此问题的明显方法不起作用。但它确实有效正如预期的那样,当我在看RStudio中的图像时。下面的代码完全重现了我的问题。
##Libraries
library(ggplot2)
set.seed(15612)
##Generate data
Year <- seq(2000,2010)
data <- -2*(Year - 2005) + 10 + runif(11,min=-3,max=3)
Title <- "Title for our graph"
xlab <- "X label"
ylab <- "Y label"
df <- data.frame(Year,data)
##Plot
##First image with small title, xlab, ylab
image1 <- ggplot(df) +
geom_line(aes(x=Year,y=data)) +
theme_bw() +
labs(title=Title,xlab=xlab,ylab=ylab)+
theme(panel.border = element_rect(fill = NA, colour="grey70"))
image1
ggsave("Image1.png",image1, width=15,height=10,units='cm')
##Second image with larger title, xlab, ylab
image2 <- image1 +
theme(axis.title.y = element_text(size = rel(1.8), angle = 90)) +
theme(axis.title.x = element_text(size = rel(1.8), angle = 00)) +
theme(plot.title = element_text(size = rel(2.0), angle = 00))
image2
ggsave("Image2.png",image2, width=15,height=10,units='cm')
dev.off()
image1
image2
These images look exactly as expected on the screen in Rstudio. Image 1 has small font sizes for the title, etc. and image 2 has larger more legible font sizes. Unfortunately, when saved as png files, they are identical, and both have small fonts for the title, x and y labels.
这些图像在Rstudio的屏幕上看起来与预期完全一致。图像1具有用于标题等的小字体大小,并且图像2具有更大的更清晰的字体大小。不幸的是,当保存为png文件时,它们是相同的,并且两者都有标题,x和y标签的小字体。
I can't (yet) post images, so if you look at these two urls, you will see the problem.
我不能(还)发布图片,所以如果你看这两个网址,你会发现问题所在。
图1 - 小标题字体
Image 2 - still a small title font, but ought to be bigger
图2 - 仍然是一个小标题字体,但应该更大
I cannot see where I am going astray. I know there are issues (or features!) with lazy evaluation in ggplot2, but I don't see where this is biting me. I would be very grateful for any help with this,
我无法看到我误入歧途的地方。我知道在ggplot2中有懒惰评估的问题(或功能!),但我没有看到这是咬我的地方。我将非常感谢任何帮助,
Regards,
Anthony Staines
1 个解决方案
#1
3
Using RStudio, I am also seeing some strange behaviour (but I need to look into the docs a bit more to decide if it is not as we should expect), however, I think you can get the output you expect by calling ggsave
, letting it use it's default plot = last.plot()
, then running the plot then calling dev.off()
between the plots. i.e.
使用RStudio,我也看到了一些奇怪的行为(但是我需要更多地查看文档以确定它是否不像我们预期的那样),但是,我认为你可以通过调用ggsave得到你期望的输出,让它使用它的默认plot = last.plot(),然后运行图,然后在图之间调用dev.off()。即
The workaround
ggsave("~/Image1.png", width=15,height=10,units='cm')
image1
dev.off()
ggsave("~/Image2.png", width=15,height=10,units='cm')
image2
dev.off()
A reproducible example of this behaviour
If we try the following example in RStudio I can get the same behaviour as the OP. Running the first code block below in RGui
3.0.0 gives us what we expect, i.e. the 3rd picture. However this is what happens in RStudio:
如果我们在RStudio中尝试以下示例,我可以获得与OP相同的行为。在RGui 3.0.0中运行下面的第一个代码块为我们提供了我们所期望的,即第三张图片。然而,这就是RStudio中发生的事情:
## Make plot and save
qp <- qplot(1:5, rnorm(5), size = I(2) )
qp
ggsave("~/Image1.png", width=15,height=10,units='cm')
## Make new plot
qp <- qplot(1:10, rnorm(10), size = I(5) )
qp
ggsave("~/Image2.png", width=15,height=10,units='cm')
At this point if we try to open the files that are saved we get:
此时如果我们尝试打开保存的文件,我们会得到:
Then we just run dev.off()
然后我们只运行dev.off()
## Without calling dev.off() plot 1 is still open and displays nothing
## Plot two is accessible from the filesystem
## Calling dev.off() we then get both plots, but BOTH plots
## use settings from plot 2
dev.off()
And we get:
我们得到:
Now if we try and save the plots by calling ggsave
then printing the plots to screen and then calling dev.off() it works as expected:
现在,如果我们通过调用ggsave尝试保存图,然后将图打印到屏幕然后调用dev.off()它按预期工作:
## Now we try calling dev.off() between plots:
qp <- qplot(1:5, rnorm(5), size = I(2) )
ggsave("~/Image1.png", width=15,height=10,units='cm')
qp
dev.off()
## Make new plot
qp <- qplot(1:10, rnorm(10), size = I(5))
ggsave("~/Image2.png", width=15,height=10,units='cm')
qp
dev.off()
We then get:
然后我们得到:
#1
3
Using RStudio, I am also seeing some strange behaviour (but I need to look into the docs a bit more to decide if it is not as we should expect), however, I think you can get the output you expect by calling ggsave
, letting it use it's default plot = last.plot()
, then running the plot then calling dev.off()
between the plots. i.e.
使用RStudio,我也看到了一些奇怪的行为(但是我需要更多地查看文档以确定它是否不像我们预期的那样),但是,我认为你可以通过调用ggsave得到你期望的输出,让它使用它的默认plot = last.plot(),然后运行图,然后在图之间调用dev.off()。即
The workaround
ggsave("~/Image1.png", width=15,height=10,units='cm')
image1
dev.off()
ggsave("~/Image2.png", width=15,height=10,units='cm')
image2
dev.off()
A reproducible example of this behaviour
If we try the following example in RStudio I can get the same behaviour as the OP. Running the first code block below in RGui
3.0.0 gives us what we expect, i.e. the 3rd picture. However this is what happens in RStudio:
如果我们在RStudio中尝试以下示例,我可以获得与OP相同的行为。在RGui 3.0.0中运行下面的第一个代码块为我们提供了我们所期望的,即第三张图片。然而,这就是RStudio中发生的事情:
## Make plot and save
qp <- qplot(1:5, rnorm(5), size = I(2) )
qp
ggsave("~/Image1.png", width=15,height=10,units='cm')
## Make new plot
qp <- qplot(1:10, rnorm(10), size = I(5) )
qp
ggsave("~/Image2.png", width=15,height=10,units='cm')
At this point if we try to open the files that are saved we get:
此时如果我们尝试打开保存的文件,我们会得到:
Then we just run dev.off()
然后我们只运行dev.off()
## Without calling dev.off() plot 1 is still open and displays nothing
## Plot two is accessible from the filesystem
## Calling dev.off() we then get both plots, but BOTH plots
## use settings from plot 2
dev.off()
And we get:
我们得到:
Now if we try and save the plots by calling ggsave
then printing the plots to screen and then calling dev.off() it works as expected:
现在,如果我们通过调用ggsave尝试保存图,然后将图打印到屏幕然后调用dev.off()它按预期工作:
## Now we try calling dev.off() between plots:
qp <- qplot(1:5, rnorm(5), size = I(2) )
ggsave("~/Image1.png", width=15,height=10,units='cm')
qp
dev.off()
## Make new plot
qp <- qplot(1:10, rnorm(10), size = I(5))
ggsave("~/Image2.png", width=15,height=10,units='cm')
qp
dev.off()
We then get:
然后我们得到: