Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embiggening a graph in ggplot2

Tags:

graph

r

ggplot2

I'm making a bunch of line graphs in R with ggplot2 and I want to save them as jpegs. However, I would like to make the graphs bigger or higher resolution, so that if you zoom in on the graphs when viewing them they don't look so pixelated.

Here's a code snippet:

library("ggplot2")

p <- ggplot(df1)

p <- p +
  geom_line(aes(time, ee_amt, colour="ee_amt"), size = 2) + 
  geom_point(aes(time, ee_amt, colour="ee_amt"), size = 2)

jpeg("G:\\Auto Parts\\sample.jpg")
  print(p)
dev.off()
like image 329
aesir Avatar asked Dec 09 '22 21:12

aesir


2 Answers

Use ggsave and specify the dpi you desire.

library(ggplot2)
df <- data.frame(x = 1:10, y = rnorm(10))
my_plot <- ggplot(df, aes(x,y)) + geom_point(size = 4)
ggsave(my_plot, file="sample.jpg", dpi = 600)
like image 182
Maiasaura Avatar answered Jan 08 '23 02:01

Maiasaura


Save plots as PostScript, and use ImageMagick convert to convert to JPEG at the desired density, e.g.:

ggsave(my_plot, file="foo.ps")

Then, to make a 300 dpi JPEG version:

$ convert foo.ps -density 300 foo.jpeg

You'll have a smaller file that you can render at any resolution you want and any bitmap format that ImageMagick supports.

If this is for the web, consider converting to SVG or PDF formats:

$ convert foo.ps foo.svg

You can embed SVG in an iframe pretty easily, and this facilitates smooth zooming with small file sizes, compared with high-resolution bitmaps.

like image 43
Alex Reynolds Avatar answered Jan 08 '23 03:01

Alex Reynolds