Last active
September 3, 2020 18:50
-
-
Save asadharis/87890c9fd0627a4987d84a682f04a4db to your computer and use it in GitHub Desktop.
Creates a png file for lines in R, can be used in figure captions
This file contains hidden or 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
# A function to create the default colors of ggplot. | |
# | |
# n: Number of different colors to generate. E.g. if you have | |
# 4 different colors then ggplot uses ggplot_cols(4). | |
ggplot_cols <- function(n) { | |
hues <- seq(15, 375, length = n + 1) | |
hcl(h = hues, l = 65, c = 100)[1:n] | |
} | |
# This function creates a line with symbol which can be used in | |
# plot captions in Latex. | |
# | |
# | |
# pch: pch of symbol, no symbol drawn if NA. | |
# col: color passed on to `col` argument, can be string, integer or | |
# color code as in output of ggplot_cols function. | |
# lty: type of line. | |
# col.label: (Optional) character can relabel color names to | |
# be more informative. e.g. col.label = c("Experiment 1", "Experiment 2") | |
# | |
# Example: myf(NA, "red", 1) creates a solid red line. | |
# Latex code to include figure: | |
# \protect\includegraphics[scale=0.4]{NA_red_1.png} | |
# | |
myf <- function(pch = NA, col = "red", lty = 2, | |
col.label = NA) { | |
if(!is.na(col.label)) { | |
name <- paste0(pch,"_", col.label, "_", lty) | |
} else { | |
name <- paste0(pch,"_", col, "_", lty) | |
} | |
png(paste0(name, ".png"), width = 1.4, height = 0.22, units = "in", res = 6000) | |
par(mar = c(0,0,0,0)) | |
plot(0:1,c(0.4,0.6), type = "n", axes = FALSE, ann = FALSE) | |
lines(0:1, rep(0.5,2), lwd = 3.5, col = col, lty = lty) | |
if(!is.na(pch)) { | |
points(c(0.33, 0.67), c(0.5,0.5), pch = pch, col = col, cex = 2.5) | |
} | |
dev.off() | |
} | |
# Same as above, but instead of plotting a line, this | |
# makes an image of a symbol alone. | |
myf2 <- function(pch = 15, col = "red", col.label = NA) { | |
if(!is.na(col.label)) { | |
name <- paste0(pch,"_", col.label) | |
} else { | |
name <- paste0(pch,"_", col) | |
} | |
png(paste0(name, ".png"), width = 0.5, height = 0.5, units = "in", res = 6000) | |
par(mar = c(0,0,0,0)) | |
plot(c(0.4,.6),c(0.4,0.6), type = "n", axes = FALSE, ann = FALSE) | |
points(0.5, 0.48, pch = pch, col = col, cex = 4.5) | |
dev.off() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment