Created
January 21, 2017 21:13
-
-
Save valentinitnelav/ccdffdabc2d18f6c08a473625d946a14 to your computer and use it in GitHub Desktop.
Plot with inwards ticks - base R
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
# ========================================================================== | |
# Example of plot for publication in base R with inwards ticks | |
# ========================================================================== | |
# ================================== | |
# create some data | |
# ================================== | |
set.seed(1) | |
DF <- data.frame(year = c(2000:2020), | |
mean = rnorm(n=21, mean=100, sd=10)) | |
# ================================== | |
# helper functions | |
# ================================== | |
# _____ Function to convert from given cm to lines of text | |
cm2line <- function(x) { | |
lh <- par('cin')[2] * par('cex') * par('lheight') | |
inch <- x/2.54 | |
inch/lh | |
} | |
# _____ Function to return a desired sequence | |
# helper for breaks along axes | |
my.breaks <- function(my.vector, step){ | |
my.min <- floor(min(my.vector)) | |
my.max <- ceiling(max(my.vector)) | |
seq(from = round(my.min/step)*step, | |
to = round(my.max/step)*step, | |
by = step) | |
} | |
# ================================== | |
# Plot | |
# ================================== | |
# ___ open graphic device with desired parameters | |
pdf(file = "Plot, type b, inwards ticks, base R.pdf", | |
width = 10/2.54, height = 7/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 | |
# ___ storing default par() for reverting later to default values | |
par.default <- par() | |
# ___ adjust plotting region | |
par(mai = c(0.9/2.54, 1.2/2.54, 0.1/2.54, 0.1/2.54)) | |
# mai - adjust margins of plotting region (Bottom, Left, Top, Right) | |
# X[cm]/2.54 = inch (or give directly inch values) | |
# ___ get some min-max values from data | |
OX.min <- floor(min(DF$year)) | |
OX.max <- ceiling(max(DF$year)) | |
OY.min <- floor(min(DF$mean)) | |
OY.max <- ceiling(max(DF$mean)) | |
# ___ plot empty frame | |
plot(x = DF$year , y = DF$mean, | |
xlab="", xlim = c(OX.min, OX.max), | |
ylab="", ylim = c(OY.min, OY.max), | |
xaxt="n", yaxt="n", type = "n") | |
# ___ add two vertical lines | |
abline(v = c(2010,2013), lty="dashed", lwd=2, col="gray") | |
# ___ add the points | |
points(x = DF$year , y = DF$mean, | |
xlab="", xlim = c(OX.min, OX.max), | |
ylab="", ylim = c(OY.min, OY.max), | |
type = "b", lty="solid", cex=1, pch=21, bg="black", | |
las = 1, font.lab=2, cex.lab=10/8, xaxt="n", yaxt="n") | |
# ___ Create custom OX axis | |
labels.X <- my.breaks(DF$year, step=5) | |
axis(side=1, at=labels.X, labels=FALSE, tck=0.02) | |
# position OX labels | |
text(x=labels.X, y=OY.min-1.5, | |
labels = labels.X, pos = 1, xpd = TRUE) | |
# x & y are graph coordinates (not cm!) | |
# x- value : adjusting for labels OX positions | |
# set the title of OX axis at x cm outwards from the plot edge - the one set with par(mai=...) above | |
title(xlab="Year",line=cm2line(0.55), font.lab=2, cex.lab=10/8) | |
# ___ Create custom OY axis | |
labels.Y <- my.breaks(DF$mean, step=10) | |
axis(side=2, at=labels.Y, labels=FALSE, tck=0.02) | |
# position OY labels | |
text(x=OX.min-1.5, y=labels.Y, | |
labels = labels.Y, xpd = TRUE) | |
# x & y are graph coordinates (not cm!) | |
# x- value : adjusting for labels OX positions | |
# set the title of OY axis at x cm outwards from the plot edge - the one set with par(mai=...) above | |
title(ylab="means",line=cm2line(0.75), font.lab=2, cex.lab=10/8) | |
# return par() to default values (par.default was saved previosly) | |
par(par.default) | |
# close the device | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment