It is not possible to increase the size of the header and x / y on the ggplot2 portion saved as a PNG file, but it works fine on the screen

I find a small but not insignificant brick wall with this frequently asked question and answer the question.

I am using Rstudio 0.97.336 and R 3.0.0 for Linux. I make a (much more complicated) schedule to fit into a document. The default size of the title and x / y labels are too small for easy reading. However, the obvious method to fix it 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. However, it works as expected when I I’m looking at images in RStudio. In the code below, my problem exactly reproduces.

##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 sharper font sizes. Unfortunately, when saved as png files, they are identical, and both have small fonts for the headers, x and y.

I cannot (yet) send images, so if you look at these two URLs you will see a problem.

Image 1 - Small Header Font

Image 2 is still a small header font, but should be larger

I don’t see what I'm losing my way with. I know there are problems (or functions!) With lazy pricing in ggplot2, but I don't see where it bites me. I would be very grateful for any help with this,

Hello,

Anthony Staines

+7
file r png ggplot2
May 6 '13 at 13:40
source share
1 answer

Using RStudio, I also see strange behavior (but I need to look into the documents a bit more to decide if this is not as expected), however I think you can get the expected result by calling ggsave , allowing it to use it by default plot = last.plot() , then running the chart, then calling dev.off() between the charts. i.e.

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 behavior

If we try the following example in RStudio, I can get the same behavior as the OP. Running the first block of code below in RGui 3.0.0 gives us what we expect, i.e. 3rd image. However, this is what happens in 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 saved files, we get: enter image description here

Then we just run 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: enter image description here

Now, if we try to save the graphs by calling ggsave , then printing the graphs on the screen and then calling dev.off (), it works as expected:

 ## 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() 

Then we get: enter image description here

+3
May 6 '13 at 13:53
source share



All Articles