Skip to content

Commit e22959d

Browse files
committed
update visualize.dist(): comparative density plots (fixes #8)
1 parent fc5edef commit e22959d

31 files changed

Lines changed: 202 additions & 113 deletions

R/globals.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ utils::globalVariables(c(
2929
"name", # <visualize.dist>
3030
"value", # <visualize.dist>
3131
"Group", # <visualize.dist>
32+
"Panel", # <visualize.dist>
33+
"Condition", # <visualize.dist>
3234
"R.Condition", # <visualize.heatmap>
3335
"R.Replicate", # <visualize.heatmap>
3436
"R.ConRep", # <visualize.heatmap>

R/visualizations.R

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,20 @@ visualize.boxplot <- function(dataSet) {
6868

6969
##----------------------------------------------------------------------------------------
7070
#'
71-
#' Average abundance distributions: Density and ECDF
71+
#' Abundance distributions
7272
#'
7373
#' @description
74-
#' Generate distribution plots of proteins' average abundance across conditions
74+
#' Generate distribution plots for protein abundance values.
75+
#' If \code{dataSet} is a single data frame, the function summarizes
76+
#' the distribution of proteins' average abundance across conditions
7577
#' and replicates, including both a kernel density estimate and an empirical
7678
#' cumulative distribution function (ECDF).
7779
#'
78-
#' @param dataSet The 2d data set of data.
80+
#' If \code{dataSet} is a list of data frames, the function produces comparative
81+
#' density plots across datasets (e.g., before vs after imputation), stratified
82+
#' by \code{R.Condition}.
83+
#'
84+
#' @param dataSet The 2d data set of data, or a list of data frames.
7985
#'
8086
#' @return
8187
#' An object of class \code{ggplot}.
@@ -86,47 +92,74 @@ visualize.boxplot <- function(dataSet) {
8692

8793
visualize.dist <- function(dataSet) {
8894

89-
plotData <- dataSet %>%
90-
pivot_longer(-c(R.Condition, R.Replicate)) %>%
91-
group_by(name) %>%
92-
summarise(mean = mean(value, na.rm = TRUE),
93-
Group = if_else(any(is.na(value)), "Missing", "Valid"),
94-
.groups = "drop")
95-
96-
if(n_distinct(plotData$Group) > 1) {
95+
if (is.data.frame(dataSet)) {
9796

98-
ggplot() +
99-
stat_density(
100-
data = plotData %>%
101-
mutate(Panel = factor("Probability Density",
102-
levels = c("Probability Density", "Cumulative Probability"))),
103-
aes(x = mean, color = Group), na.rm = TRUE, geom = "line") +
104-
stat_ecdf(
105-
data = plotData %>%
106-
mutate(Panel = factor("Cumulative Probability",
107-
levels = c("Probability Density", "Cumulative Probability"))),
108-
aes(x = mean, col = Group), na.rm = TRUE, geom = "line", pad = FALSE) +
109-
facet_wrap(~ Panel, scales = "free_y") +
110-
labs(x = "Average Abundance", y = NULL) +
111-
theme_bw() +
112-
theme(legend.position = "bottom")
97+
plotData <- dataSet %>%
98+
pivot_longer(-c(R.Condition, R.Replicate)) %>%
99+
group_by(name) %>%
100+
summarise(mean = mean(value, na.rm = TRUE),
101+
Group = if_else(any(is.na(value)), "Missing", "Valid"),
102+
.groups = "drop")
113103

114-
} else {
104+
if(n_distinct(plotData$Group) > 1) {
105+
106+
ggplot() +
107+
stat_density(
108+
data = plotData %>%
109+
mutate(Panel = factor("Probability Density",
110+
levels = c("Probability Density", "Cumulative Probability"))),
111+
aes(x = mean, color = Group), na.rm = TRUE, geom = "line") +
112+
stat_ecdf(
113+
data = plotData %>%
114+
mutate(Panel = factor("Cumulative Probability",
115+
levels = c("Probability Density", "Cumulative Probability"))),
116+
aes(x = mean, col = Group), na.rm = TRUE, geom = "line", pad = FALSE) +
117+
facet_wrap(~ Panel, scales = "free_y") +
118+
labs(x = "Average Abundance", y = NULL) +
119+
theme_bw() +
120+
theme(legend.position = "bottom")
121+
122+
} else {
123+
124+
ggplot() +
125+
stat_density(
126+
data = plotData %>%
127+
mutate(Panel = factor("Probability Density",
128+
levels = c("Probability Density", "Cumulative Probability"))),
129+
aes(x = mean), na.rm = TRUE, geom = "line") +
130+
stat_ecdf(
131+
data = plotData %>%
132+
mutate(Panel = factor("Cumulative Probability",
133+
levels = c("Probability Density", "Cumulative Probability"))),
134+
aes(x = mean), na.rm = TRUE, geom = "line", pad = FALSE) +
135+
facet_wrap(~ Panel, scales = "free_y") +
136+
labs(x = "Average Abundance", y = NULL) +
137+
theme_bw()
138+
139+
}
140+
} else if (is.list(dataSet)) {
115141

116-
ggplot() +
117-
stat_density(
118-
data = plotData %>%
119-
mutate(Panel = factor("Probability Density",
120-
levels = c("Probability Density", "Cumulative Probability"))),
121-
aes(x = mean), na.rm = TRUE, geom = "line") +
122-
stat_ecdf(
123-
data = plotData %>%
124-
mutate(Panel = factor("Cumulative Probability",
125-
levels = c("Probability Density", "Cumulative Probability"))),
126-
aes(x = mean), na.rm = TRUE, geom = "line", pad = FALSE) +
142+
nm <- names(dataSet)
143+
if (is.null(nm) || any(nm == "")) {
144+
nm <- paste("Data", seq_along(dataSet))
145+
}
146+
147+
plotData <- lapply(seq_along(dataSet), function(k) {
148+
dataSet[[k]] %>%
149+
pivot_longer(cols = -c(R.Condition, R.Replicate),
150+
names_to = "name", values_to = "value") %>%
151+
mutate(Panel = nm[k], .before = 1)
152+
}) %>%
153+
bind_rows() %>%
154+
rename(Condition = R.Condition) %>%
155+
mutate(Panel = factor(Panel, levels = nm))
156+
157+
ggplot(plotData, aes(value, color = Condition)) +
158+
stat_density(geom = "line", na.rm = TRUE) +
127159
facet_wrap(~ Panel, scales = "free_y") +
128-
labs(x = "Average Abundance", y = NULL) +
129-
theme_bw()
160+
labs(x = "Abundance", y = "Density") +
161+
theme_bw() +
162+
theme(legend.position = "bottom")
130163

131164
}
132165

0 commit comments

Comments
 (0)