如何在多个小提琴图中将中点加到每个小提琴上?

时间:2020-08-31 20:21:04

标签: r ggplot2 violin-plot

我正在使用ggplot2对自己的数据集进行分组的小提琴图。数据集包含3个变量的350个观测值(5个位置的7个场景,每个场景有10个重复项),部分内容如下所示:

我的数据集的一部分: enter image description here

我使用的代码在这里:

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

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

enter image description here

我对这个数字非常满意,但是我添加的中点似乎聚集在情节的中间,而不是每个小提琴中。我猜这是因为它们应该在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")

1 个答案:

答案 0 :(得分:0)

解决方案可能会涉及向摘要层添加躲闪位置。下面的虚拟数据示例:

d <- structure(list(SNP = c("rs1", "rs2", "rs3", "rs4", "rs7", "rs8", 
                            "rs9", "rs10", "rs11", "rs362"), 
                    X = c(5, 1, 6, 2, 11, 3, 1.2, 
                          2.2, 2.2, 3.2),
                    Y = c("aa", "bb", "aa", "bb", "ft", "hg", "ff", 
                          "cc", "yh", "hyu")), 
               class = "data.frame", 
               row.names = c(NA, -10L))