Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't increase title and x/y label size in a ggplot2 plot saved as a PNG file, but it works fine on screen

Tags:

file

r

ggplot2

png

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.

##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.

I can't (yet) post images, so if you look at these two urls, you will see the problem.

Image 1 - small title font

Image 2 - still a small title font, but ought to be bigger

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,

Regards,

Anthony Staines

like image 543
astaines Avatar asked May 06 '13 13:05

astaines


People also ask

How do I save a ggplot file as a PNG?

In most cases ggsave() is the simplest way to save your plot, but sometimes you may wish to save the plot by writing directly to a graphics device. To do this, you can open a regular R graphics device such as png() or pdf() , print the plot, and then close the device using dev. off() .

How do I make text bigger in ggplot2?

How can I change the default font size in ggplot2? Set base_size in the theme you're using, which is theme_gray() by default. The base font size is 11 pts by default. You can change it with the base_size argument in the theme you're using.

How do I change X label to R in ggplot?

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

What are the default dimensions that Ggsave ()` saved your image as?

2.1. The default size of the saved image is equal to the size of Plots pane (the “graphics device”) in RStudio, which can be found with dev. size() . Notice that the result of dev. size() and the message we receive when saving the plot with ggsave() give the same dimensions.


1 Answers

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.

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:

## 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: 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 and save the plots by calling ggsave then printing the plots to 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()

We then get: enter image description here

like image 143
Simon O'Hanlon Avatar answered Nov 10 '22 20:11

Simon O'Hanlon