Last active
December 9, 2016 15:42
-
-
Save valentinitnelav/c6b24f36bace05c6741f182db18bb371 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# This script presents a simple example of plotting using several graphical devices | |
# For a list of graphical devices offered in R just run ?Devices | |
# Note that journals usually accept PDF as vector format and TIFF for images | |
# PDF is the easiest way to use when it comes to creating graphs in vector format. | |
# Also the EPS (postscript) format is widely accepted by journals. | |
# For TIFF files, use the "LZW compression" option when saving. | |
# That will significantly reduce the file size, without adversely affecting the image quality | |
# (http://esapubs.org/esapubs/preparation.htm#Ill). | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# create some random example data | |
x <- rnorm(5000) | |
y <- rnorm(5000) | |
# ================================================================================== | |
# A) Simple plotting of scatterplot (no device) | |
# ================================================================================== | |
plot(x,y, main = "", | |
col = rgb(red = 0, green = 0, blue = 0, alpha = 0.15), | |
pch = 16) | |
# col = rgb() - creates RGB colors and alpha gives color transparency | |
# ================================================================================== | |
# B) Plotting scatterplot with PDF graphics device driver | |
# ================================================================================== | |
# This is the most practical way I often use. PDF files can be imported in Inkscape | |
# (https://inkscape.org/en/, is open source) and then can be | |
# saved to a multitude of formats. | |
# | |
# open the graphic device with desired parameters | |
pdf(file = "Scatterplot as PDF (10 by 9 cm).pdf", | |
width = 10/2.54, height = 9/2.54, | |
family = "Times", pointsize = 8) | |
# Since "width" and "height" need to be in inches, one can still use cm | |
# and divide by 2.54 to make the conversion to inch. | |
# family="Times" refers to the font family (here: Times New Roman). | |
# Check this link for some family fonts: http://www.statmethods.net/advgraphs/parameters.html | |
# pointsize = 8 refers to the point size used, here (Times New Roman) 8 | |
# plot the scatterplot | |
plot(x,y, main = "", | |
col = rgb(red = 0, green = 0, blue = 0, alpha = 50, maxColorValue = 255), | |
pch = 16) | |
# col = rgb() - creates RGB colors and alpha gives color transparency | |
# check ?rgb for further help | |
# close the device | |
dev.off() | |
# ================================================================================== | |
# C) Plotting scatterplot with enhanced metafile (EMF) graphics device driver | |
# ================================================================================== | |
# 'EMF' can be imported as vector graphics in both OpenOffice and Microsoft Office | |
# NOTE: The EMF format does not allow for color transparency. | |
# Attempting to use a transparent color will result in a warning message | |
# (from: https://cran.r-project.org/web/packages/devEMF/devEMF.pdf) | |
# Solution - plot as PDF, then import in Inkscape (https://inkscape.org/en/, is open source) | |
# and then export to a multitude of formats, EMF included. | |
# library "devEMF" is required | |
if (!require("devEMF")) { | |
install.packages("devEMF") | |
library("devEMF") | |
} | |
# open the graphic device with desired parameters | |
emf(file = "Scatterplot as EMF (10 by 9 cm).emf", | |
width = 10/2.54, height = 9/2.54, | |
family = "Times", pointsize = 8) | |
# Since "width" and "height" need to be in inches, one can still use cm | |
# and divide by 2.54 to make the conversion to inch. | |
# family="Times" refers to the font family (here: Times New Roman). | |
# pointsize = 8 refers to the point size used, here (Times New Roman) 8 | |
# plot the scatterplot | |
plot(x,y, main = "", | |
col = rgb(red = 0, green = 0, blue = 0, maxColorValue = 255), | |
pch = 16) | |
# col = rgb() - creates RGB colors | |
# check ?rgb for further help | |
# close the device | |
dev.off() | |
# ================================================================================== | |
# D) Plotting scatterplot with postscript graphics device driver | |
# ================================================================================== | |
# NOTE: The postscript format does not allow for color transparency. | |
# For alternatives check 'cairo_ps' function from the grDevices package | |
# (http://stackoverflow.com/questions/4001316/how-do-i-preserve-transparency-in-ggplot2) | |
# Or plot as PDF, then import in Inkscape (https://inkscape.org/en/, is open source) | |
# and then export to a multitude of formats (e.g. SVG or EPS) | |
# open the graphic device with desired parameters | |
postscript(file = "Scatterplot as postscript (10 by 9 cm).ps", | |
width = 10/2.54, height = 9/2.54, | |
family = "Times", pointsize = 8) | |
# Since "width" and "height" need to be in inches, one can still use cm | |
# and divide by 2.54 to make the conversion to inch. | |
# family="Times" refers to the font family (here: Times New Roman). | |
# pointsize = 8 refers to the point size used, here (Times New Roman) 8 | |
# plot the scatterplot | |
plot(x,y, main = "", | |
col = rgb(red = 0, green = 0, blue = 0, maxColorValue = 255), | |
pch = 16) | |
# col = rgb() - creates RGB colors | |
# check ?rgb for further help | |
# close the device | |
dev.off() | |
# ================================================================================== | |
# E) Plotting scatterplot with TIFF graphics device drive | |
# ================================================================================== | |
# For TIFF files, use the "LZW compression" option when saving the files. | |
# That will significantly reduce the file size, without adversely affecting the image quality | |
# (http://esapubs.org/esapubs/preparation.htm#Ill). | |
# open the graphic device with desired parameters | |
tiff(file = "Scatterplot as tiff (10 by 9 cm).tif", | |
width = 10, height = 9, units = "cm", res = 600, | |
family = "Times", pointsize = 8, | |
compression = "lzw", type = "cairo") | |
# units = "cm" specifies the units for width and height | |
# res = 600 - the nominal resolution in ppi | |
# family = "Times" refers to the font family (here: Times New Roman). | |
# pointsize = 8 refers to the point size used, here (Times New Roman) 8 | |
# compression = "lzw" reduces the file size, without adversely affecting the image quality | |
# (advice from http://esapubs.org/esapubs/preparation.htm#Ill) | |
# type = "cairo" - plotting using cairographics; https://en.wikipedia.org/wiki/Cairo_(graphics) | |
# plot the scatterplot | |
plot(x,y, main = "", | |
col = rgb(red = 0, green = 0, blue = 0, alpha = 50, maxColorValue = 255), | |
pch = 16) | |
# col = rgb() - creates RGB colors and alpha gives color transparency | |
# check ?rgb for further help | |
# close the device | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment