I am making simple plots in R and want the resulting images to be very high resolution. I tried just specifying large height and width parameters in the output image code, but this just makes a large image with fuzzy data points.
My current code looks like this:
png(filename="simple_graphic.png", width=5600, height=1000)
depth_plot <- c(a large list of data)
plot(depth_plot, type="o", col="blue", log="y",)
dev.off()
Any help optimizing this code to make the resolution of each data point higher is much appreciated!
You can also specify the width and the height in pixels. In R GUI you will need to go to File → Save as and select the type of file you prefer. If you select Jpeg , you can also specify the quality of the resulting image.
You can either print directly a ggplot into PNG/PDF files or use the convenient function ggsave() for saving a ggplot. The default of ggsave() is to export the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.
dev. off shuts down the specified (by default the current) device. If the current device is shut down and any other devices are open, the next open device is made current.
Method 1: Displaying only values using text() function in Plot. In this function user just need to call the text() function with the specified parameters into it after calling the plot() function in R language and this process will lead to a plot of the provided points by the user with the values in the plot.
There are several possible answers to this question, and the comments are being careful to try to teach you where to find the answer vice just giving it to you. I'll spoil that by providing that to which they allude, but also present another question:
The answer: use res=300
(or higher) in your call to png. From help(png)
, it states that the default ppi (pixels per inch) is 72, below what you want for fine-resolution graphs. 300 is decent for most printers and screens, YMMV.
png(filename="simple_graphic.png", res=300) # perhaps width/height as well
# ...
dev.off()
Another question: in what format can I best show my graph? This is often overlooked by many people making graphs with R, and is unfortunate but easy to remedy: use a vector-based image format. If you are using Powerpoint or Word, then you may be better off making an enhanced metafile format (EMF) file.
install.packages('devEMF') # just once
library(devEMF)
emf(filename="simple_graphic.emf", width=1024, height=800)
# ...
dev.off()
If you are able to use the image in a scalable vector graphic (SVG) format (e.g., Inkscape, Scribus, ...), then merely use svg(...)
in place of emf(...)
. Same options/rules apply.
Similarly, if you're LaTeX, you can go straight to pdf using pdf(...)
(same syntax).
NB: one benefit of going to one of these vector-based (vice raster-based such as PNG) image formats is that if you want/need to finesse the graph in ways that you don't know how in R, you can open the EMF/SVG/PDF in something like Inkscape in order to easily modify lines (color, thickness), text (font, size, location), boxes, arrows, etc. And Inkscape will allow you to export into EMF, SVG, PDF, PNG, ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With