Skip to content

Instantly share code, notes, and snippets.

@hiraksarkar
Created March 5, 2024 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiraksarkar/789a535c8ee84f209e510b134627f48d to your computer and use it in GitHub Desktop.
Save hiraksarkar/789a535c8ee84f209e510b134627f48d to your computer and use it in GitHub Desktop.
ggdump example
library(ggplot2)
library(dplyr)
library(ggbump)
tmp.subset = lr_df_merged %>% filter(ligand %in% c('WNT9A','ARTN', 'ANGPTL2') )
tmp.subset = tmp.subset |> arrange(ligand,desc(copula_coeff))
to_nodes = distinct(tmp.subset, receptor) |> mutate(to_y = row_number())
num_senders = (distinct(tmp.subset, ligand) %>% dim)[[1]]
from_nodes = distinct(tmp.subset, ligand) |> mutate(from_y = round(dim(to_nodes)[[1]])/(num_senders+1) + row_number()-1+0.5)
fig.size(8,10)
df = tmp.subset |> arrange(desc(copula_coeff)) |>
## generate mapping type/case variables
group_by(ligand) |>
mutate(n_dest = n()) |>
ungroup() |>
group_by(receptor) |>
mutate(n_origin = n(),
min_weight = min(copula_coeff)) |>
ungroup() |>
mutate(value_case = case_when(copula_coeff <= 0 ~ "negative",
copula_coeff > 0 ~ "positive")) |>
left_join(tribble(~value_case, ~line_type, ~font_type,
"positive", "solid", "bold",
"negative", "dashed", "italic"),
by = "value_case") |>
left_join(from_nodes, by = "ligand") |>
left_join(to_nodes, by = "receptor") |>
## add x-coordinates
mutate(from_x = 0,
to_x = 10) |>
## give each from-out instruction a unique id
mutate(idx = row_number())
df |>
ggplot(aes(x = from_x , xend = to_x, y = from_y, yend = to_y, group = idx)) +
## edges as sigmoid curves with line type
geom_sigmoid(aes(linetype = I(line_type))) +
# to/from nodes
scale_y_reverse() +
geom_label(
data = df %>% distinct(ligand, .keep_all = TRUE),
aes(
x = from_x-0.1,
label=ligand),size=5,hjust = 1) +
#geom_label(aes(x = from_x - 0.5, y = to_y, label=receptor),hjust = 1) +
geom_label(aes(x = to_x , y = to_y, label=receptor, fill = value_case),hjust = 0) +
# edge labels
geom_label(data = df,
aes(x = (((from_x + to_x) / 2) + to_x) / 2,
y = to_y,
label = round(copula_coeff,2),
alpha = copula_coeff),
fill = "gray") +
scale_fill_manual(values = wesanderson::wes_palette(n = 4, name = "GrandBudapest2")) +
scale_color_manual(values = wesanderson::wes_palette(n = 4, name = "GrandBudapest2")) +
cowplot::theme_minimal_grid(font_size = 14, line_size = 0) +
theme(legend.position = "none",
panel.grid.major = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank()
) +
scale_x_continuous(limits = c(-1, 11)) +
labs(x = "Copula Coefficients", y = 'Ligand', fill = "Target", fontface="Source")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment