#' Lines plot fo rpublication in ggplot2
#'
#' @param data data
#' @param group.column name of group column
#' @param label.column name of column containing labels for proteins
#' @param x.y.ratio ratio of x- and y-axis steps
#' @param main.color color of lines
#' @param highlight.color color of highlighted protein lines
#' @param highlight.proteins vector giving containing proteins to highlight
#' @param variables.label.fun function to transform accession IDs to labels
#' @param alpha transparency of lines
#' @param xlab x-axis label
#' @param ylab y-axis label
#'
#' @return
#' @export
#'
#' @import ggplot2
#'
plot_lines <- function(data, group.column = "observations", label.column, x.y.ratio, main.color = "grey",
highlight.color = "red", highlight.proteins = NULL, variables.label.fun = p2g,
alpha = 0.8, xlab = NULL, ylab = NULL) {
# Rename groups column
data <- data %>%
dplyr::rename(groups_x = !!group.column)
# Melt data frame
data_melt <- suppressMessages(reshape2::melt(data = data))
# Add color
data_melt <- data_melt %>%
dplyr::mutate(color = factor(ifelse(variable %in% highlight.proteins, highlight.color, main.color),
levels = c(main.color, highlight.color)))
# Arrange proteins
data_melt <- data_melt %>%
dplyr::arrange(match(color, c(main.color, highlight.color))) %>%
dplyr::mutate(variable = factor(variable, levels = unique(variable)))
#
data_melt <- data_melt %>%
dplyr::mutate(label = variables.label.fun(as.character(variable))) %>%
dplyr::mutate(label = ifelse(groups_x %in% last(levels(data_melt$groups_x)) & variable %in% highlight.proteins,
label,
NA_character_))
#
y.max <- max(data_melt$value)
y.min <- min(data_melt$value)
range <- y.max - y.min
text.size <- 6
# line.width.factor
lwf <- 1 / (ggplot2::.pt * 72.27 / 96)
# Plot
p <- ggplot(data = data_melt, mapping = aes(x = groups_x, y = value, group = variable, color = color, label = label)) +
geom_path(alpha = alpha, size = 0.25 * lwf, lineend = "round", linejoin = "round") +
scale_y_continuous(expand = rep(range * 0.01, 2)) +
scale_x_discrete(expand = c(0.1, 0, 0, 2)) +
scale_color_manual(values = c(main.color, highlight.color)) +
xlab(xlab) +
ylab(ylab) +
theme(plot.margin = unit(c(1, 0.5, 1, 0.5), "cm"),
text = element_text(size = unit(text.size, "pt")),
axis.text = element_text(size = unit(text.size, "pt"),
color = "black"),
panel.background = element_blank(),
axis.line.y.left = element_line(size = 0.25 * lwf),
axis.line.x.bottom = element_line(size = 0.25 * lwf),
axis.ticks = element_line(size = 0.25 * lwf),
legend.position = "none") +
ggrepel::geom_text_repel(
size = text.size / .pt,
force = 0.5,
nudge_x = 0.5,
direction = "y",
hjust = 0,
segment.size = 0.25 * lwf,
segment.curvature = 0,
segment.angle = 45
)
#
if (hasArg(x.y.ratio)) {
p <- p +
coord_fixed(ratio = x.y.ratio)
}
# Plot
print(p)
#ggsave(filename = "plot.pdf", width = 3, height = 3, units = "in", dpi = "print")
# Return
return(invisible(p))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.