Embigene graph in ggplot2

I am doing a bunch of line graphs in R with ggplot2 and I want to save them as jpeg. However, I would like to make the graphs larger or higher, so if you zoom in on the graphs when viewing them, they do not look so bright.

Here's the 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() 
+4
source share
3 answers

Use ggsave and specify the desired dpi .

 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) 
+10
source

Save the graphics as PostScript and use ImageMagick convert to convert to JPEG with the desired density, for example:

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

Then, to make a 300 dpi version of JPEG:

 $ convert foo.ps -density 300 foo.jpeg 

You will have a smaller file that you can display at any resolution and any bitmap format supported by ImageMagick.

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

 $ convert foo.ps foo.svg 

You can easily embed SVG in an iframe , and this makes it easy to smoothly scale with small file sizes compared to high-resolution bitmaps.

+4
source

I do not recommend png (): ggsave () or jpeg () are the best options. To directly control resolution using png (), use:

 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) W = 1680 H = 1050 png("test.png", width = W, height = H) print(p) graphics.off() 

The same should work for jpeg ().

0
source

Source: https://habr.com/ru/post/1432383/


All Articles