SAVING A GRAPH

Saving a graph

Two options.

Option 1: Use your mouse

Almost all graphs can be saved with a right click, as a postscript or metafile. A bitmap version can also be copied to be incorporated in another document (or saved through paint for example).

Option 2: Using code

The code version is a little bit more convoluted, but offers more options. It can be used to save your plots in BMP, JPEG, PNG and TIFF format files (with the functions “bmp()”,”jpeg()”,”png()” and “tiff()” respectively). The procedure is a little bit strange because you won’t see anything onscreen. What happens is that R is opening a “virtual” window where it will plot your graph and then save it. That’s why I recommend that before using the function, you actually try it onscreen. Each function has its own options that I invite you to check out through the help files. Those will help you to set the dimensions and resolution of the final picture among other things. That being said, here is the general procedure to save your graph…

x=seq(-10,10,0.1)   # creating a sequence of numbers going from -10 to +10 by increments of 0.1 through the function "seq()"
y= 0.3*x^2+2.7

First, open this virtual window where you will save your file, while naming it (with the correct extension).

jpeg(file = "myplot.jpeg")

Then give all the instruction you need to plot your plot:

plot(x,y= 0.3*x^2-2.3,type="l")
abline(h=0,col="red")
abline(v=0,lwd=3)
abline(a=2,b=1,lty="dashed")
title(main="example")

Noticed how nothing appears onscreen? That’s normal, everything is happening in the virtual window. Once done, we can close this virtual window with the function “dev.off()“. This will save our graph to the previously specified file.

dev.off()

INTRODUCTION

Graphs so pretty, you'll want to display them on your fridge.

GETTING INFORMATION

Or how to interact with your graph!

THE “PLOT()” FUNCTION

A picture is worth a thousand words. Here is how to make a graph worth twice that.

MORE GRAPHICAL FUNCTIONS

Want a histogram? A boxplot? A barplot? You came to the right place!

ADDING ELEMENTS

A look at putting finishing touches to your masterpiece.

A LITTLE BIT OF 3D?

2D is so 1990. 3D is the future.

CONCLUSION

Go. Be proud. You have the right to.