geposanui/scatter_plot.R

60 lines
1.6 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-09-16 00:06:54 +02:00
for (gene_id in genes[, id]) {
2021-08-26 12:50:03 +02:00
cluster_species <- unlist(results[gene == gene_id, cluster_species])
data[id == gene_id, in_cluster := species %in% cluster_species]
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") +
scale_shape_discrete(
name = "Part of cluster",
breaks = c(TRUE, FALSE),
labels = c("Yes", "No")
) +
geom_point(
mapping = aes(
x = species,
y = distance / 1000000,
color = name,
shape = in_cluster
),
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
}