Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Last active October 7, 2021 13:14
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save valentinitnelav/ec7c253c80f4e045f4bd1c9b2920d7bd to your computer and use it in GitHub Desktop.
Save valentinitnelav/ec7c253c80f4e045f4bd1c9b2920d7bd to your computer and use it in GitHub Desktop.
Plot with inwards ticks - ggplot
# ==========================================================================
# Example of plot for publication with inwards ticks in ggplot
# ==========================================================================
library(ggplot2)
# ==================================
# create some data
# ==================================
set.seed(1)
DF <- data.frame(year = c(2000:2020),
mean = rnorm(n=21, mean=100, sd=10))
# ==================================
# helper function
# ==================================
# Function to return a desired sequence breaks along axes
my.breaks <- function(my.vector, step){
my.min <- floor(min(my.vector))
my.max <- ceiling(max(my.vector))
my.seq <- seq(from = round(my.min/step)*step,
to = round(my.max/step)*step,
by = step)
return(my.seq)
}
# ==================================
# Plot
# ==================================
ggplot(data = DF, aes(x = year, y = mean)) +
# add two vertical lines
geom_vline(xintercept=c(2010,2013), linetype="dashed", color="gray", lwd=.5) +
# add lines
geom_line(lwd=.4) +
# add points
# note: use "stroke" to adjust the white border thickness of the point
# in order to give the same effect as in base-r plot `type=b`
geom_point(size=2, shape=21, fill="black", colour="white", stroke=1.2) +
# set axes titles and labels
scale_x_continuous(name = "Year", breaks = my.breaks(DF$year, step=5)) +
scale_y_continuous(name = "means", breaks = my.breaks(DF$mean, step=10)) +
# Theme adjustments:
theme_bw() + # eliminate default background
theme(panel.grid.major = element_blank(), # eliminate major grids
panel.grid.minor = element_blank(), # eliminate minor grids
# set font family for all text within the plot ("serif" should work as "Times New Roman")
# note that this can be overridden with other adjustment functions below
text = element_text(family="serif"),
# adjust X-axis title
axis.title.x = element_text(size = 10, face = "bold"),
# adjust X-axis labels; also adjust their position using margin (acts like a bounding box)
# using margin was needed because of the inwards placement of ticks
# http://stackoverflow.com/questions/26367296/how-do-i-make-my-axis-ticks-face-inwards-in-ggplot2
axis.text.x = element_text(size = 8, margin = unit(c(t = 2.5, r = 0, b = 0, l = 0), "mm")),
# adjust Y-axis title
axis.title.y = element_text(size = 10, face = "bold"),
# adjust Y-axis labels
axis.text.y = element_text(size = 8, margin = unit(c(t = 0, r = 2.5, b = 0, l = 0), "mm")),
# length of tick marks - negative sign places ticks inwards
axis.ticks.length = unit(-1.4, "mm"),
# width of tick marks in mm
axis.ticks = element_line(size = .3)
)
# save as pdf
ggsave("Plot, type b, inwards ticks - ggplot.pdf", width=10, height=7, units="cm")
# save as png
ggsave("Plot, type b, inwards ticks - ggplot.png", width=10, height=7, units="cm", dpi=300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment