Discussion:
How to overlay plots with the same resposne variable but different explanatory variables?
Li Yanxian
2018-07-19 14:46:37 UTC
Permalink
Hi all,

I made Fig.1 using the attached dataset and the code below but I couldn't
overlap the boxplot with the scatter plot as different
explanatory variables were used to generate the figure. How can I overlay
the plots so that the dots and mean bar are within the boxplot (like Fig.2,
the mean bar was manually added for illustration)?

## R code

library(dplyr)
library(ggplot2)
library(ggrepel)

df <- read.csv("file_path", header =T, na.strings=c(""))
head(df)

#convert numeric variable to string
df <- within(df, {
Sample_ID <- as.character(Sample_ID)
Net_Pen <- as.character(Net_Pen)
})

# Define a function to identify potential outliers
is_outlier <- function(x) {
x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 *
IQR(x)
}

#make the plot
df %>%
select(-Comments) %>%
na.omit() %>%
group_by(Gene_Name) %>%
mutate(Cq_Mean_std = scale(Cq_Mean)) %>%
mutate(outlier = is_outlier(Cq_Mean_std)) %>%
ggplot(aes(x=Diet, y=Cq_Mean_std,label = ifelse(outlier, Sample_ID, NA)))
+
geom_boxplot(aes(x=Gene_Name), outlier.shape = NA) +
geom_point(aes(fill = Diet),size = 3, shape = 21,position =
position_jitterdodge(0.2)) +
stat_summary(fun.y = mean, fun.ymin = mean, fun.ymax = mean,
geom = "crossbar", width = 0.5) +
facet_wrap(~ Gene_Name, nrow = 1,scales="free_y") +
geom_label_repel() +
theme_bw()

## Fig.1
Fig.2

<https://lh3.googleusercontent.com/-qppyszhV9Wo/W1Cf1tTsqBI/AAAAAAAAAGI/1dAqhV8IlzoM2ZIEF0GDW0Vts-z2N1S4ACLcBGAs/s1600/Fig.1.tiff>

<https://lh3.googleusercontent.com/-r5XlkioYVCo/W1ChqDlPPkI/AAAAAAAAAGU/6p_rV62pi9UCRQmSD1fKDJxL6QmRJ5m5wCLcBGAs/s1600/Fig.2.tiff>
<https://lh3.googleusercontent.com/-qppyszhV9Wo/W1Cf1tTsqBI/AAAAAAAAAGI/1dAqhV8IlzoM2ZIEF0GDW0Vts-z2N1S4ACLcBGAs/s1600/Fig.1.tiff>
Thanks,
<https://lh3.googleusercontent.com/-r5XlkioYVCo/W1ChqDlPPkI/AAAAAAAAAGU/6p_rV62pi9UCRQmSD1fKDJxL6QmRJ5m5wCLcBGAs/s1600/Fig.2.tiff>
Yanxian
<https://lh3.googleusercontent.com/-r5XlkioYVCo/W1ChqDlPPkI/AAAAAAAAAGU/6p_rV62pi9UCRQmSD1fKDJxL6QmRJ5m5wCLcBGAs/s1600/Fig.2.tiff>
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...