library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ lubridate 1.9.4 ✔ tibble 3.2.1
## ✔ purrr 1.2.0 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
library(kableExtra)
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
library(patchwork)
library(tidyr)
library(boot)
library(scales)
Within R there is the possibility to create a graph consisting of two already existing graphs ( Dr Wiles may refer to this as many things such as a hydra chart, combination chart, Tiamat, Ghedorah chart, etc.). To do this, we must first understand how to create the two graphs you desire which can be seen in previous chapters, such as Pheatmap, Violin, etc. In this example, we use a density plot and a whisker plot that we created using the following code.
data_outlier <- df_bw %>%
group_by(conditions) %>%
mutate(
Q1 = quantile(abs, 0.25),
Q3 = quantile(abs, 0.75),
IQR = Q3 - Q1,
lower_fence = Q1 - 1.5 * IQR,
upper_fence = Q3 + 1.5 * IQR,
is_outlier = abs < lower_fence | abs > upper_fence
) %>%
ungroup()
outlier_data <- data_outlier %>%
filter(is_outlier) %>%
mutate(outlier_label = paste0(ver_pos))
value_min <- min(filtered_data_df$abs)
value_max <- max(filtered_data_df$abs)
density_A <- density(filtered_data_df$abs[filtered_data_df$conditions == "SD30"])
density_B <- density(filtered_data_df$abs[filtered_data_df$conditions == "SD37"])
# Extract the 'y' (density) values from the calculated densities
y_values_A <- density_A$y
y_values_B <- density_B$y
# Combine and find the overall min and max density
all_y_values <- c(y_values_A, y_values_B)
min_density <- min(all_y_values)
max_density <- max(all_y_values)
x_range <- max(filtered_data_df$abs) - min(filtered_data_df$abs)
y_range <- max_density - min_density
appropriate_ratio <- x_range/y_range
p_boxplot <- ggplot(ordered_df,
aes(x=abs,y=conditions, color = conditions))+
geom_boxplot(outlier.shape = 15, outlier.stroke = 1, outlier.colour = "black")+
xlim(value_min, value_max)+
geom_segment(data=data_fence,
aes(x=whisker_up,
xend=whisker_up,
y=as.numeric(conditions) - 0.25,
yend=as.numeric(conditions) + 0.25))+
geom_segment(data=data_fence,
aes(x=whisker_lo,
xend=whisker_lo,
y=as.numeric(conditions) - 0.25,
yend=as.numeric(conditions) + 0.25))+
geom_text(data = outlier_data,
aes(x = abs, y = conditions, label = ver_pos),
color = "black",
vjust = -1,
size = 3)+
scale_color_manual(breaks = c("SD30", "SD37"), values = c("SD30" = "#F8766D","SD37" ="#A3A500"))+
scale_x_continuous(limits = c(0.1,0.4))
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
p_density <- ggplot(filtered_data_df,
aes(x=abs, color=conditions)
)+
facet_wrap(~ conditions, ncol = 1)+
xlim(value_min, value_max)+
geom_density(aes(colors = conditions), show.legend = FALSE)+
scale_color_manual(values = c("#F8766D", "#A3A500"))+
scale_x_continuous(limits = c(0.1, 0.4))+
theme(strip.text = element_blank())
## Warning in geom_density(aes(colors = conditions), show.legend = FALSE):
## Ignoring unknown aesthetics: colours
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
p_boxplot + p_density +plot_layout(nrow = 1, ncol = 2)
(p_boxplot / p_density)
combined_plot <- (p_boxplot / p_density +
plot_layout(widths = c(6, 0.1), heights = c(1, 3)))
combined_plot
filtered_data_df$dens <- filtered_data_df %>%
mutate(dens = 6.5)
filtered_data_df %>%
ggplot(x = abs, color = conditions)+
facet_wrap(~ conditions, ncol = 1)+
xlim(value_min, value_max)+
geom_boxplot(aes(x = abs, y = 6.25, color = conditions), width = 2, outlier.shape = 15, outlier.color = "black")+
geom_density(aes(x = abs, color = conditions), show.legend = FALSE)+
scale_x_continuous(limits = c(0.1, 0.4))+
labs(y = "density")+
geom_segment(data=data_fence,
aes(x=whisker_up,
xend=whisker_up,
y=as.numeric(6.25) - .75,
yend=as.numeric(6.25) + .75),
color = (values = c( "#A3A500", "#F8766D")))+
geom_segment(data=data_fence,
aes(x=whisker_lo,
xend=whisker_lo,
y=as.numeric(6.25) - .75,
yend=as.numeric(6.25) + .75),
color = (values = c("#A3A500", "#F8766D")))+
geom_text(data = outlier_data,
aes(x = abs, y = 6.25, label = ver_pos),
color = "black",
vjust = -1,
size = 3)+
scale_color_manual(values = c("#F8766D", "#A3A500"))+
theme(strip.text = element_blank())
## Warning in fortify(data, ...): Arguments in `...` must be used.
## ✖ Problematic arguments:
## • x = abs
## • color = conditions
## ℹ Did you misspell an argument name?
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.