In RStudio, we can adjust the size of 4 windows (or panels): text editor, console, environment/history, files/plots/packages/help/viewer.
I frequently export plots from RStudio and paste it in MS Word or save it as an image file. My concern is that the size of the plot is different every time because I often adjust the size of panels as I need.
Is there any way that I adjust the panels' sizes to the same sizes that I did before after I randomly adjust the panel sizes?
If I can specify the panel size by numbers, then I can do it, but RStudio does not provide such feature.
This code writes your plot in PNG format (good for web), with a specified width, height, and resolution. (In this case, 400 pixels / 72 pixels per inch = 5.56 inches across--every time). See here for more detail and other formats.
png(file="YOUR_PLOT",width=400,height=350,res=72)
Related
As a beginner on R I manage to produce correct relational graphs thanks to R. They are about correct in the visualization space but of very bad quality when I export them in PDF or in JPEG / PNG. The image is not centered, a part of the legend is missing, the graph is very small or blurred etc.
How do you proceed for the export?
Thanks in advance!
I am looking for the right handling or settings to export the graph visualizations produced in R?
I understood that I had to set up the viewer space but via the code but have no idea how to do it...
Here an example how to write a plot to a PNG file. The plot "commands" are embedded in png(....)and dev.off(). Several options are available to configure size and resolution.
png("myfile.png", width=1600, height=1200, res=300) # good for LaTeX or Word
#png("myfile.png", width=800, height=600, res=150) # good for Powerpoint or Impress
plot(iris$Sepal.Length, iris$Petal.Length)
dev.off()
Some hints:
width and height are given in pixels
res influences nominal resolution and font size (play with it)
use at least 300 dpi (dots per inch). For centimeters, the number of pixels = 300/2.54 * width in cm
professionals use 600 or even 1200 pixels per inch, but then .docx and .pptx files will dramatically increase
1600 x 1200px is good for 13.3 x 10 cm size in the printed document
If you work with LaTeX, it is in most cases better to use PDF for the figures. Another very good idea is to use Markdown for the text. Then, figures are automatically embedded.
When I output the graph in R, the size (width and length) is always changing. So whenever I output graphs, I change the size by myself.
For example, I need a consistent graph size to insert into my document, but sometimes the default value of width and height is not the same when I try to export, particularly on different PC. How can I fix W:500. H:400 graph size all the time?
Many thanks!!
In order to get consistency, you should set the size of the graphics device yourself before plotting. For example, in Windows, you can use
windows(width=5.5, height=5, title="Controlled Size")
## NOW make your plot
to get a window that is always 5.5 inches wide by 5 inches high.
Other devices use minor variations for setting the size. You can check out other devices by looking at the help page help(Devices)
For example, bmp, jpeg, png and tiff allow you to set the number of pixels, not just the number of inches.
I currently have a plot that I would like to save with certain resolution values. I am using the pdf() function in R with:
pdf("my_plot.pdf", height=5, width=10)
plot(....)
dev.off()
From this, I get a pdf which when I use Mac's Info in Finder, it gets:
I am wondering how I can set the height and width parameters in pdf() to obtain match a certain resolution above, such as 811 x 478. Thanks.
If height is 811 and width is 478 pixels (although I later decided those were probably reversed) then you could divide those by a typical dpi to get a height and width in inches which is the unit that the pdf device expects:
pdf("my_plot.pdf", height=811/300, width=478/300)
plot(....)
dev.off()
I'm wondering, however, whether the first number is a height or width? I suspect you may be seeing width as the first value. Let's look at the help page:
?pdf
And look at some examples with Finder and Preview:
I'm looking at small pdf files and seeing that an icon is reported to have a resolution of 128 × 128 in Finder.app and Preview reports that it is 1.78 × 1.78 inches, so it probably was created as a 72 dpi document. The pdf() help page says that pointsize can be used to affect that parameter and defaults to 12 to specify 1/72 inches.
So perhaps you will want:
pdf("my_plot.pdf", width=811/72, height=478/72)
I currently am one of the few R users in my company, which consists predominantly of stata users. One problem I've had with making plots using ggplot2 is that the default (theme_grey()) settings have much smaller axis font and a smaller legend than what is found in stata. Moreover, in presentations I find people have trouble reading the legend and titles from a distance.
I'm aware that ggplot2 has a theming system that allows for relative sizing. What I'd ideally like to do is to create a new default theme that I'd apply to all my plots that would make legends and axis titles larger. Importantly, however, very often the graphs I make have varying dimensions when output to pdf (e.g. 8 inch x 10 inch) or ( 10 inch x 13 inch). Since I'd like to apply this theme globally, I need it to produce good/easy to read output irrespective of the dimensions I specify when outputting to pdf.
I'd really appreciate any suggestions for how to do this/how to approach the problem using ggplot2's theming system.
Thanks!
The theme system can easily scale all the (themed) text, but not in a device sized aware way. The various theme sets, including theme_bw(), have an argument base_size which is the baseline size, in points, of fonts to use for the text. Some text is rendered at that size, and some is rendered at sizes relative to that (for example, axis labels and legend labels are rendered at 80% of the baseline size). So by specifying the base_size argument, you can scale all that text.
However, the base_size is in absolute points, not in a size which is relative to the device size. So the larger the device size, the smaller the text is relative to that.
A journal we are sending an article to is asking for the following:
To ensure the best reproduction quality of your figures we would appreciate high resolution files. All figures should preferably be in TIFF or EPS format... and should have the following resolution:
Graph: 800 - 1200 DPI
Photo: 400 - 800 DPI
Color (only CMYK): 300 - 400 DPI (DPI = dots per inch)
Since I am sending a graph, I am trying to save it using tiff.
Here is the code I am using:
tiff(filename = "c:\\aaa.tiff",
res = 800, pointsize = 2)
plot(1:100)
dev.off()
But sadly, it produces a very "bulky" image - and if I where to not use pointsize = 2, I would get the error message:
Error in plot.new() : figure margins too large
Any suggestions?
One thing you should do is use the compression parameter.
For instance:
tiff("outfile.tif", compression = "lzw")
LZW is a lossless compression format, so you will not lose any data, but you will definitely reduce filesize.
short answer:
It's a bulky file because the higher the resolution raster file (TIFF) then the larger the file.
The other option, EPS, is like PDF or PS. Just save in any of those. That's a vector image format and one they said was acceptable. It will be more compact and of higher quality (unless your figure contains an enormous number of points or lines.)
long answer:
One format they asked for is TIFF, a raster format. It saves the information as a value representing each pixel in the image. If the image is 100 DPI and 5" square then that's 500x500 and it saves 250000 pixels of information. As the resolution of the image gets higher and the image size stays constant then the points of information needed goes up. At 200 DPI it's 1000x1000 and 1e6 pixels of information. This explains why your file is so bulky.
In order to have a quality printed image at 5" square you would need about 300-400 DPI (dots per inch). That way you won't see each of those individual little pixels that make up the image. If it's at a lower resolution then the image is still 5" x 5" but each individual square block (pixel) that makes up the image is so large you can see them. That's why they asked for that resolution. Otherwise, text and lines in your graphs would appear blocky, jaggy, or blurry. It's why text on your computer screen (only around 100 DPI these days) isn't nearly as high quality as printed on a piece of paper (usually at least 300 dpi).
The other format they asked for was EPS. This is encapsulated postscript. Since it's a single page file PS, or postscript, would be equivalent. And, PDF is also similar. These are all vector formats, essentially the same vector format (they can contain raster format images too but let's ignore that).
A vector format file saves the information about how the image was drawn but not about each pixel. If you have an x-axis 3" long placed 0.5" from the bottom of the image then the vector file knows this description. Each feature of the graph is described in the file. Because of this, for simple drawings it is vastly more compact than any raster representation at a given image quality. Furthermore, it scales in size without losing quality. You simply ask it to draw the image at 6" x 6" instead of 5" x 5" and it scales each of the instructions for drawing the components of the image up as well. Therefore, even though the PDF or EPS file will be more compact, it will print at the highest quality.
Send a PDF or PS file and don't worry about the resolution.
When you increase resolution (res), the effective size of a plot decreases, and so the margin error jumps out. Try to make bigger picture with greater width and height (in theory, to make a plot of a same virtual size, it should be new_(width|height)=res/72*old_(width|height).
Still, as romunov suggested, it is a better idea to use ImageMagick; you won't loose quality.
I would suggest you to use devEval from the R.devices library.
I had this issue myself and solved it thanks to this :
options("devEval/args/path"=file.path(Folder_of_Output))
devEval("tiff", name="Name_output", width=120, height=800, {
barplot(data, main="imagetitle");
legend("top", legend =c("text of legend"))
})
For me it worked like a charm and the file was saved directly without issue.