For many, the main reason to use R is to generate really good-looking or at least informative graphics. However, while it is easy to find information on how to make an individual plot, it can take some time to find out how to get them out into the world. Here is my four-step program to turning your plot into a graphic-file.
In the following I will use my present favorite plot from here as an example.
1. Set your options
R allows you to set many general options for your plots, e.g. the margins and whether or not a box should be drawn around most of which are the documentation here.
My favorites are:
- mfrow: To combine several plots into one (not necessary for the exaple).
- mar: To control the margins of the plot (not necessary for the exaple).
- las: To rotate the axis-labels (not necessary for the example)
2. Make your plot
Well this part is the most heterogeneous, just take a peek at the gallery to get some inspiration, or dive into ggplot2 for a very comprehensive graphic-framework that also helps you to add legends.
pie(c(1,1), labels="", col=c("black", "white"), main="Your options according to Yoda", init.angle=90)
3. Add a legend
R has a built-in function to add legends. The full documentation can be found here, The options I use almost every time are:
- x,y: To tell R where to put the legend. Usually I use the name for the location (e.g. “top left”), instead of x and y-coordinates.
- legend: To add some descriptions for the colors/line-types/shadings.
- fill: To select the colors or alternatively “lty” for the line-type
- bty: To get rid of the box around the legend
legend("right", c("do", "do not", "try"), fill=c("black", "white", "gold"), bty="n", cex=1.4)
4. Save it to a file
R has various options to save files, as documented here. I most often save them as png, as the file-size for tiffs is extremely large at the same quality. This allows you to set the options
- filename: well something with a ”.png” at the end
- width and height: To control the scale and of the image.
- units: To
- resolution: To Journals love images with at least 300 dpi.
- bg: To have non-transparent background simply use “white”.
dev.print(png, "yoda.png", width=8, height=6, units="in", res=300, bg="white")
Enjoy the complete script
par(mar=c(2,0,2,2)) pie(c(1,1), labels="", col=c("black", "white"), main="Your options according to Yoda", init.angle=90) legend("right", c("do", "do not", "try"), fill=c("black", "white", "gold"), bty="n", cex=1.4) dev.print(png, "yoda.png", width=8, height=6, units="in", res=300, bg="white") |
1. Configura tus opciones
Mis favoritos son:* mfrow: Para combinar varios diagramas en un (no es necesario para el ejemplo).
* mar: Para controlar los márgenes del diagrama (no es necesario para el ejemplo)
* las: Para rotar los ejes-etiquetas (no es necesario para el ejemplo).
2. Haz tu diagrama
Esta parte es la mas heterogénea. Puedes visitar nuestra galería para inspirarte o recorrer ggplot2 para ver una estructura grafica (graphic-framework) muy completa que además te permita agregar notas.
pie(c(1,1), labels="", col=c("black", "white"), main="Your options according to Yoda", init.angle=90)
3. Agregar una nota
R ha incorporado funciones para agregar notas. Puedes encontrar la documentación completa aquí. Las opciones que uso generalmente son:
- x,y: Para decirle a R donde colocar la nota. Generalmente uso el nombre para la localización (por ejemplo: “top left”) en lugar de coordenadas x-y.
- legend: Para agregar algunas descripciones sobre colors/line-types/shadings (colores/tipo de líneas/sombreado).
- fill: Para elegir los colores o “lty” para el tipo de línea.
- bty: Para deshacerse de la “caja” alrededor de las notas.
legend(“right”, c(“do”, “do not”, “try”), fill=c(“black”, “white”, “gold”), bty=”n”, cex=1.4)
legend("right", c("do", "do not", "try"), fill=c("black", "white", "gold"), bty="n", cex=1.4)
4. Guardarlo en un archivo
R tiene varias opciones para guardar archivos, tal como esta documentado aquí. Por lo general guardo los archivos en formato png ya que los archivos tiffs son extremadamente pesados y de igual calidad. Esto te permite configurar las siguientes opciones:
- filename: El nombre del archivos con un “.png” al final
- width and height: Para controlar la escala de la imagen.
- units: Para
- resolution: Los Journals adoran las imágenes con al menos 300 dpi.
- bg: Para tener un fondo no transparente debes usar simplemente “white”.
dev.print(png, "yoda.png", width=8, height=6, units="in", res=300, bg="white")
Disfruta el Script completo!
par(mar=c(2,0,2,2)) pie(c(1,1), labels="", col=c("black", "white"), main="Your options according to Yoda", init.angle=90) legend("right", c("do", "do not", "try"), fill=c("black", "white", "gold"), bty="n", cex=1.4) dev.print(png, "yoda.png", width=8, height=6, units="in", res=300, bg="white") |