R语言 如何为多小提琴图中的每把小提琴添加中值点?

ukdjmx9f  于 2023-04-09  发布在  其他
关注(0)|答案(2)|浏览(293)

我正在使用ggplot 2对我自己的数据集进行分组小提琴图。该数据集包含350个观察结果(5个位置的7个场景,每个场景有10个重复),其中一部分看起来像这样:
我的数据集的一部分:

我使用的代码在这里:

DF = read.csv("C:\\Users\\lqy\\Desktop\\Pilot_data.csv", na.strings = "---", header = TRUE)
DF = data.frame(DF)

DF$Scenarios = as.integer(DF$Scenarios)
    
figure = ggplot(DF, aes(x = Location, y = Recovery, fill = Scenarios)) +
  geom_violin() + 
  stat_summary(fun="median",geom="point") +
  labs(x="Locations", y="Days to 90% recovery") +
  theme(axis.text = element_text(size = 10)) +
  theme(axis.title = element_text(size = 10)) +
  theme(legend.position = "right")
figure

从这些代码中,我有一个看起来像这样的图:

我对这个图很满意,但是我添加的中间点似乎聚集在图的中间,而不是在每个小提琴上。我猜这是因为它们应该在x轴上的点的正上方对齐。但是有没有一种方法可以将中间点放在这种图中的每个相应小提琴上?
非常感谢你为我回答这个问题!
附加:数据集在这里(通过代码dput(DF[sample(nrow(DF),45),])获取)

structure(list(Scenarios = c(8L, 2L, 2L, 2L, 10L, 5L, 5L, 10L, 
10L, 3L, 10L, 1L, 2L, 5L, 8L, 2L, 1L, 3L, 1L, 8L, 10L, 4L, 8L, 
2L, 4L, 3L, 8L, 10L, 1L, 1L, 10L, 5L, 3L, 8L, 8L, 5L, 8L, 5L, 
10L, 1L, 8L, 8L, 8L, 3L, 10L), Location = c("Total_Catchment", 
"Sec_51", "Sec_53", "Total_Catchment", "Sec_55", "Sec_55", "Sec_51", 
"Sec_51", "Sec_54", "Total_Catchment", "Sec_55", "Sec_55", "Sec_54", 
"Sec_53", "Sec_51", "Sec_55", "Sec_53", "Sec_55", "Sec_54", "Total_Catchment", 
"Sec_51", "Sec_53", "Sec_55", "Total_Catchment", "Sec_54", "Total_Catchment", 
"Sec_53", "Sec_53", "Sec_51", "Sec_54", "Sec_53", "Sec_51", "Sec_53", 
"Sec_54", "Sec_54", "Sec_55", "Sec_55", "Sec_54", "Sec_51", "Sec_51", 
"Sec_51", "Total_Catchment", "Sec_51", "Sec_55", "Sec_53"), Recovery = c(316.5, 
839.5, 179.5, 277.5, 923.5, 664.5, 494.5, 639.5, 273.5, 327.5, 
830.5, 714.5, 357.5, 300.5, 504.5, 752.5, 265.5, 535.5, 208.5, 
303.5, 564.5, 339.5, 766.5, 396.5, 273.5, 271.5, 185.5, 370.5, 
825.5, 191.5, 186.5, 582.5, 364.5, 326.5, 332.5, 901.5, 706.5, 
187.5, 577.5, 680.5, 506.5, 301.5, 559.5, 713.5, 324.5)), row.names = c(20L, 
121L, 163L, 37L, 329L, 348L, 103L, 112L, 273L, 52L, 322L, 309L, 
240L, 187L, 76L, 338L, 155L, 339L, 253L, 69L, 133L, 158L, 342L, 
2L, 235L, 45L, 146L, 161L, 106L, 239L, 189L, 117L, 157L, 265L, 
258L, 299L, 321L, 215L, 98L, 127L, 132L, 27L, 111L, 283L, 203L
), class = "data.frame")
kpbpu008

kpbpu0081#

解决方案可能涉及到在摘要层中添加减淡位置。下面是虚拟数据的示例:

library(ggplot2)

df <- data.frame(
  Locations = rep(c("Sec_51", "Sec_53"), each = 1000),
  Recovery = rnorm(2000),
  Scenarios = rep(rep(LETTERS[1:5], each = 200), 2)
)

ggplot(df, aes(Locations, Recovery,
               group = interaction(Locations, Scenarios))) +
  geom_violin(aes(fill = Scenarios)) +
  stat_summary(fun = median, geom = "point",
               position = position_dodge(0.9))

xfyts7mz

xfyts7mz2#

如何使用Vlnplot函数对以下代码执行此操作:

seurat_01 <- readRDS("/home/mitralab/data2/20210204-integrated_seurat_T1exp_T10exp_Tolexp_0.2.Rds")

使用原始计数聚类基因表达图谱0.2

library(patchwork)
library(cowplot)
library(ggplot2)
library(RColorBrewer)
library(Seurat)
gene_list <- c("NDUFV1","NDUFV2","NDUFS1","NDUFS2","NDUFS3","NDUFS8","NDUFS7",
               "MT-ND1","MT-ND2","MT-ND3","MT-ND4","MT-ND4L","MT-ND5","MT-ND6")

data <- seurat_01
DefaultAssay(data) <- "RNA"
plot_list <- list()
for (i in gene_list) {
  plot_list[[i]] <- FeaturePlot(data, features = i, split.by = "sample", cols = c("grey","blue"))
}
vplot_list <- list()
data$clusternum <- Idents(data)
for (i in gene_list) {
  vplot_list[[i]] <- VlnPlot(data, features = i, split.by = "sample", group.by = "clusternum",
    pt.size = 0, combine = FALSE) + stat_summary(fun = median, geom = 'point', size = 2, color = 'black')
    + 
   scale_y_continuous('Expression Level') + 
   scale_x_discrete("Identity")
}
for (i in gene_list) {
  ggsave(paste0("/home/mitralab/featureplot/","map_", i, "_0.2_raw.pdf"),plot = plot_list[[i]], width = 11, height = 8.5)
  pdf(paste0("/home/mitralab/vlnplot/","vin_", i, "_0.2_raw.pdf"), width = 11, height = 8.5)
   print(vplot_list[[i]])
  dev.off()
}

相关问题