在分组的小提琴图上绘制单个点

Graphing individual points on a grouped violin plot

经过大量查找,我似乎无法找到我问题的确切答案,所以我想问一下。

我想使用 ggplot2 为我有两个条件(“控制 RNAi”和“mex-6 RNAi”)的时间进程制作分组小提琴图。每个数据点都来自 3 个不同的副本(数据帧中的“蠕虫”因素),所以我的数据帧格式看起来像这样(“mean_mex6”是绘制的 Y 值):

mean_mex6 RNAi Time Worm
2.4102356 Control RNAi 2hr worm1
0.8332575 Control RNAi 2hr worm1
2.5093177 Control RNAi 2hr worm1
0.8792359 Control RNAi 2hr worm1
1.2570116 Control RNAi 2hr worm1
1.0671826 Control RNAi 2hr worm1

数据框中还有更多行,但我上面显示的数据只是来自“worm1”在“2 小时”时间点对“Control RNAi”的一些数据点。

我希望在小提琴图上的每个 RNAi 组中绘制所有单独的点,但我希望绘制它们以便每个“蠕虫”样本的每个数据点都具有与其他蠕虫不同的颜色。我已经能够创建一个分组的小提琴图,其中绘制了所有单独的点,但没有为每个单独的蠕虫样本进行颜色编码:

library(ggplot2)

ggplot(compiled_allhours, aes(x=Time, y=mean_mex6, colour=RNAi)) + 
  geom_violin(trim=FALSE) +
  scale_x_discrete(limits=c("2hr", "4hr","6hr", "8hr","24hr")) + ##This chooses which data to plot and orders them
  geom_quasirandom(aes(x=Time, y=mean_mex6, colour = RNAi), dodge.width = 0.9, varwidth = TRUE) +
  ggtitle(expression(paste(italic("mex-6"), " nuclear signal", " - WT"))) +
  theme(plot.title = element_text(hjust = 0.5)) + ##Centers the title of the plot
  xlab("Time") +
  ylab(expression(paste("normalized ", italic("mex-6"), " nuclear signal (A.U.)")))

我附上了这个情节的图片。[情节 1] https://i.stack.imgur.com/yvfYW.png

如果我尝试用蠕虫给各个点上色,会发生以下情况:

library(ggplot2)

ggplot(compiled_allhours, aes(x=Time, y=mean_mex6, colour=RNAi)) + 
  geom_violin(trim=FALSE) +
  scale_x_discrete(limits=c("2hr", "4hr","6hr", "8hr","24hr")) + ##This chooses which data to plot and orders them
  geom_quasirandom(aes(x=Time, y=mean_mex6, colour = Worm), dodge.width = 0.9, varwidth = TRUE) +
  ggtitle(expression(paste(italic("mex-6"), " nuclear signal", " - WT"))) +
  theme(plot.title = element_text(hjust = 0.5)) + ##Centers the title of the plot
  xlab("Time") +
  ylab(expression(paste("normalized ", italic("mex-6"), " nuclear signal (A.U.)")))

[情节 2] https://i.stack.imgur.com/aVJwG.png

所以基本上,我想要第二个图,但是所有这些点都合并到小提琴图中,如第一个图中所示。感谢您的帮助!

问题是第二个图中的 geom_violingeom_quasirandom 应用了不同的分组。因此,点和小提琴以不同方式“闪避”并且彼此不对齐。

要获得您想要的结果,您必须使用 group 美学,即按 TimeRNAi 使用例如interaction.

此外,我没有将 Worm 映射到颜色上,而是将其映射到 fill 上(对我来说更有意义,因为 Worm 和 RNai 是不同的变量)并使用了填充点 (shape=21)这给出了不同的传说。但随时可以通过切换回 color.

来改变它

使用更真实的随机数据集,包括不同变量的“所有”选项试试这个:

set.seed(42)
compiled_allhours <- data.frame(
  mean_mex6 = runif(200),
  RNAi = sample(c("Control RNAi", "mex-6 RNAi"), 200, replace = TRUE),
  Time = sample(c("2hr", "4hr", "6hr", "8hr", "24hr"), 200, replace = TRUE),
  Worm = sample(c("worm1", "worm2", "worm3"), 200, replace = TRUE)
)
library(ggplot2)
library(ggbeeswarm)

ggplot(compiled_allhours, aes(x=Time, y=mean_mex6, group=interaction(RNAi, Time))) + 
  geom_violin(aes(color = RNAi), trim=FALSE) +
  scale_x_discrete(limits=c("2hr", "4hr","6hr", "8hr","24hr")) + ##This chooses which data to plot and orders them
  geom_quasirandom(aes(x=Time, y=mean_mex6, fill = Worm), shape = 21, color = "transparent", dodge.width = 0.9, varwidth = TRUE) +
  ggtitle(expression(paste(italic("mex-6"), " nuclear signal", " - WT"))) +
  theme(plot.title = element_text(hjust = 0.5)) + ##Centers the title of the plot
  xlab("Time") +
  ylab(expression(paste("normalized ", italic("mex-6"), " nuclear signal (A.U.)")))

reprex package (v0.3.0)

于 2021 年 1 月 4 日创建