Skip to content

Commit

Permalink
fixed dotplot by genesetsize
Browse files Browse the repository at this point in the history
  • Loading branch information
tetomonti committed Oct 19, 2023
1 parent 9fceb9d commit 3387a04
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions R/hyp_dots.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
pval_cutoff=1,
fdr_cutoff=1,
val=c("fdr", "pval"),
title="") {
title="")
{
# Default arguments
val <- match.arg(val)
size_by <- match.arg(size_by)
Expand All @@ -37,7 +37,6 @@
dplyr::filter(fdr <= fdr_cutoff) %>%
dplyr::select(label)
})

# Take top genesets
labels <- names(sort(table(unlist(multihyp_dfs)), decreasing=TRUE))
if (!is.null(top)) labels <- head(labels, top)
Expand All @@ -50,7 +49,7 @@
hyp_df <- hyp_obj$data
hyp_df[hyp_df$label %in% labels, c("label", val), drop=FALSE]
})

# merge dataframes ('cbind')
df <- suppressWarnings(Reduce(function(x, y) merge(x, y, by="label", all=TRUE), dfs))
colnames(df) <- c("label", names(dfs))
rownames(df) <- df$label
Expand All @@ -63,7 +62,6 @@
} else {
rownames(df) <- factor(label.abrv, levels=label.abrv)
}

if (val == "pval") {
cutoff <- pval_cutoff
color.label <- "P-Value"
Expand All @@ -72,36 +70,36 @@
cutoff <- fdr_cutoff
color.label <- "FDR"
}

df.melted <- reshape2::melt(as.matrix(df))
colnames(df.melted) <- c("label", "signature", "significance")
df.melted$size <- 1

if (size_by == "significance") {
df.melted$size <- df.melted$significance
}

if (size_by == "genesets") {
geneset.sizes <- lapply(multihyp_data, function(hyp_obj) {
hyp_obj$data[, c("label", "geneset")]
}) %>%
do.call(rbind, .) %>%
dplyr::distinct(label, .keep_all=TRUE) %>%
dplyr::pull(geneset, label)
df.melted$size <- geneset.sizes[df.melted$label]
#df.melted$size <- geneset.sizes[df.melted$label]
names(geneset.sizes) <- substr(names(geneset.sizes), 1, abrv)
df.melted$size <- geneset.sizes[match(df.melted$label,names(geneset.sizes))]
}

p <- df.melted %>%
dplyr::filter(significance <= cutoff) %>%
ggplot(aes(x=signature, y=label, color=significance, size=size)) +
geom_point() +
scale_color_continuous(low="#114357", high="#E53935", trans=.reverselog_trans(10)) +
labs(title=title, color=color.label) +
theme(plot.title=element_text(hjust=0.5),
axis.title.y=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_text(angle=45, hjust=1))

dplyr::filter(significance <= cutoff) %>%
ggplot(aes(x = signature, y = label, color = significance, size = size)) +
geom_point() +
scale_color_continuous(low = "#114357", high = "#E53935", trans = .reverselog_trans(10)) +
labs(title = title, color = color.label) +
theme(
plot.title = element_text(hjust = 0.5),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)
)
if (size_by == "none") {
p <- p + guides(size="none")
}
Expand Down

0 comments on commit 3387a04

Please sign in to comment.