geposanui/scatter_plot.R

49 lines
1.3 KiB
R
Raw Normal View History

2021-06-24 22:38:16 +02:00
library(data.table)
library(ggplot2)
#' Draw a scatter plot containing gene positions.
2021-08-26 12:50:03 +02:00
#'
#' @param results Results from [`process_input()`].
2021-08-29 13:25:12 +02:00
#' @param species Species to be displayed.
2021-09-16 00:06:54 +02:00
#' @param genes Genes to be displayed.
#' @param distances Distance data to display.
scatter_plot <- function(results, species, genes, distances) {
if (nrow(genes) < 1) {
2021-08-26 12:50:03 +02:00
return(ggplot())
}
species_ids <- species[, id]
2021-06-24 22:38:16 +02:00
2021-08-26 12:50:03 +02:00
data <- merge(
2021-09-16 00:06:54 +02:00
genes[, .(id, name)],
distances[species %in% species_ids],
2021-08-26 12:50:03 +02:00
by.x = "id", by.y = "gene"
)
2021-06-24 22:38:16 +02:00
2021-08-26 12:50:03 +02:00
ggplot(data) +
scale_x_discrete(
name = "Species",
breaks = species$id,
2021-09-16 00:06:54 +02:00
labels = species$name
2021-08-26 12:50:03 +02:00
) +
scale_y_continuous(
name = "Distance to telomeres [Mbp]",
limits = function(x) {
if (x[2] < 15) {
c(0, 15)
} else {
x
}
}
) +
scale_color_discrete(name = "Gene") +
geom_point(
mapping = aes(
x = species,
y = distance / 1000000,
2021-09-30 12:54:40 +02:00
color = name
2021-08-26 12:50:03 +02:00
),
size = 5
2021-08-29 13:25:12 +02:00
) +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
2021-06-24 22:38:16 +02:00
}