R-studio: Create and store beautiful plots: ready for publication !
Every scientist needs to produce great papers. What makes the paper great is an awesome idea, but the nice graphics and plots can help you to greatly communicate the results, that your awesome idea is based on.
Here I would like to share my approach how to produce a simple plot, decide about your ideal width:height ratio, and export the plot as a .pdf file to your location.
Produce some dummy data:
(plot example is adopted from http://www.sthda.com/english/wiki/add-legends-to-plots-in-r-software-the-easiest-way)
x<-1:10 y1=x*x y2=2*y1
Create a plot:
plot(x, y1, type="b", pch=19, col="red", xlab="x", ylab="y", main = "my title")
# Add a line lines(x, y2, pch=18, col="blue", type="b", lty=2)
# Add a legend legend(1, 95, legend=c("Line 1", "Line 2"), col=c("red", "blue"), lty=1:2, cex=0.8)
You should get back something like this:
The plot you get back is not really pretty, not well adjusted, and it is a pain to "Export it" in a pretty way. Maybe it would look better if we can interactively update how the plot size and the ratio between height and width? Let's try!
Firstly, open the new plot window:
On Windows, type windows(), on iOS type quartz().
Now we can play with different sizes of windows. Just add height = number, width = number in your parameters. As I am on Windows, I'm using windows(height = 4, width = 5). Play with different values of height and width.
The default values are in inches. 1 inch = 2.54 cm. For two columns fitting plot I use maximal width of 7 inches, 3.5 inches for one columns fitting plot.
R studio always generates the new windows, when the line is run. You should get something like this (your new windows might be overlapping on the bottom toolbar; click on them to see them all):
Those are my empty plots, where I would like to display my data.
If you know, what size of plot is appropriate, you can close all plots (windows) by running graphics.off().
To display the data in the plot, just place the windows(height = 4, width = 5) code above your plot:
windows(height = 4, width = 5) # set the height and width of your plot
plot(x, y1, type="b", pch=19, col="red", xlab="x", ylab="y", main = "my title") # Add a line
lines(x, y2, pch=18, col="blue", type="b", lty=2)
# Add a legend legend(1, 95, legend=c("Line 1", "Line 2"), col=c("red", "blue"), lty=1:2, cex=0.8)
Here is the result:
I am perfectly happy with the way how my plot look like. I want to export it exactly in the same way as it is: the same ratio between the the width and height. My favorite format is a .pdf, as it keeps the quality of the plot, is easy to manipulate and really ready for submission.
Thus, I need to add another part of code to my plot:
pdf("C:/my_location/Figure 1.pdf", height = 4, width = 5)
This code defines the format (pdf), my location to store the final file (my_location), and the height and width of my windows. Don't forget the set the same values as you used for windows(height = 4, width = 5)!!
I place my pdf() code after the windows(), and before the plot(), for logistic reasons.
windows(height = 4, width = 5) # set the height and width of your plot
# export final .pdf pdf("C:/my_location/Figure 1.pdf", height = 4, width = 5)
plot(x, y1, type="b", pch=19, col="red", xlab="x", ylab="y", main = "my title") # Add a line lines(x, y2, pch=18, col="blue", type="b", lty=2) # Add a legend legend(1, 95, legend=c("Line 1", "Line 2"), col=c("red", "blue"), lty=1:2, cex=0.8)
If you check now your final location, you will discover that not new .pdf file was created. That's because the plot is still open: we need to close it.
Use dev.off() after the plot() to close it.
windows(height = 4, width = 5) # set the height and width of your plot
# export final .pdf pdf("C:/my_location/Figure 1.pdf", height = 4, width = 5)
plot(x, y1, type="b", pch=19, col="red", xlab="x", ylab="y", main = "my title") # Add a line lines(x, y2, pch=18, col="blue", type="b", lty=2) # Add a legend legend(1, 95, legend=c("Line 1", "Line 2"), col=c("red", "blue"), lty=1:2, cex=0.8)
# add to close the plot
dev.off()
Tadaaaa !! my final plot is exported as a nice .pdf, with proportions I am perfectly happy about !
Let's close all unused plots by single line code, instead of clicking X on every single one... Run graphics.off() to close all of them.
Note at the end: to quickly check, how does my data look like in one or another height:width ratio scenario, I just leave a hashtag # before the #pdf(). Then I can run the whole code from windows() to plot, or just grab the pdf() without hashtag to export my pdf.
Final code:
# generate data
x<-1:10
y1=x*x
y2=2*y1
# create plot:
#test the windows size
windows(height = 4, width = 5) # set the height and width of your plot
# export final .pdf # pdf("C:/my_location/Figure 1.pdf", height = 4, width = 5)
plot(x, y1, type="b", pch=19, col="red", xlab="x", ylab="y", main = "my title") # Add a line lines(x, y2, pch=18, col="blue", type="b", lty=2) # Add a legend legend(1, 95, legend=c("Line 1", "Line 2"), col=c("red", "blue"), lty=1:2, cex=0.8)
# close the plot to successful .pdf export
dev.off()