使用ggplot2,我可以創建一個帶有重疊點的小提琴圖,并且可以使用 連接成對的點geom_line()。
library(datasets)
library(ggplot2)
library(dplyr)
iris_edit <- iris %>% group_by(Species) %>%
mutate(paired = seq(1:length(Species))) %>%
filter(Species %in% c("setosa","versicolor"))
ggplot(data = iris_edit,
mapping = aes(x = Species, y = Sepal.Length, fill = Species))
geom_violin()
geom_line(mapping = aes(group = paired),
position = position_dodge(0.1),
alpha = 0.3)
geom_point(mapping = aes(fill = Species, group = paired),
size = 1.5, shape = 21,
position = position_dodge(0.1))
theme_classic()
theme(legend.position = "none",
axis.text.x = element_text(size = 15),
axis.title.y = element_text(size = 15),
axis.title.x = element_blank(),
axis.text.y = element_text(size = 10))

該see軟體包包括在geom_violindot()其組成點旁邊繪制對半小提琴圖的功能。我發現這個功能在繪制大量點時很有用,這樣小提琴就不會被遮擋。
library(see)
ggplot(data = iris_edit,
mapping = aes(x = Species, y = Sepal.Length, fill = Species))
geom_violindot(dots_size = 0.8,
position_dots = position_dodge(0.1))
theme_classic()
theme(legend.position = "none",
axis.text.x = element_text(size = 15),
axis.title.y = element_text(size = 15),
axis.title.x = element_blank(),
axis.text.y = element_text(size = 10))

現在,我想添加geom_line()以geom_violindot()連接成對的點,如第一張圖片所示。理想情況下,我希望點在里面,小提琴在外面,這樣線條就不會與小提琴相交。geom_violindot()包括flip引數,它采用指定要翻轉的幾何圖形的數字向量。
ggplot(data = iris_edit,
mapping = aes(x = Species, y = Sepal.Length, fill = Species))
geom_violindot(dots_size = 0.8,
position_dots = position_dodge(0.1),
flip = c(1))
geom_line(mapping = aes(group = paired),
alpha = 0.3,
position = position_dodge(0.1))
theme_classic()
theme(legend.position = "none",
axis.text.x = element_text(size = 15),
axis.title.y = element_text(size = 15),
axis.title.x = element_blank(),
axis.text.y = element_text(size = 10))

As you can see, invoking flip inverts the violin half, but not the corresponding points. The see documentation does not seem to address this.
Questions
- How can you create a
geom_violindot()plot with paired points, such that the points and the lines connecting them are "sandwiched" in between the violin halves? I suspect there is a solution that uses David Robinson'sGeomFlatViolinfunction, though I haven't been able to figure it out. - In the last figure, note that the lines are askew relative to the points they connect. What position adjustment function should be supplied to the
position_dotsandpositionarguments so that the points and lines are properly aligned?
uj5u.com熱心網友回復:
不確定是否將 geom_violindot 與 see 包一起使用。但是您可以將 geom_half_violon 和 geom_half_dotplot 與 gghalves 包結合使用,并對資料進行子集化以指定方向:
library(gghalves)
ggplot(data = iris_edit[iris_edit$Species == "setosa",],
mapping = aes(x = Species, y = Sepal.Length, fill = Species))
geom_half_violin(side = "l")
geom_half_dotplot(stackdir = "up")
geom_half_violin(data = iris_edit[iris_edit$Species == "versicolor",],
aes(x = Species, y = Sepal.Length, fill = Species), side = "r")
geom_half_dotplot(data = iris_edit[iris_edit$Species == "versicolor",],
aes(x = Species, y = Sepal.Length, fill = Species),stackdir = "down")
geom_line(data = iris_edit, mapping = aes(group = paired),
alpha = 0.3)
請注意,配對中的線不會正確對齊,因為點圖正在對每個觀察結果進行分箱,然后延長點線 - 配對線僅對應于 aes 中定義的 x 值,而不對應于線中點的位置.
uj5u.com熱心網友回復:
根據評論 - 這不是對您問題的直接回答,但我相信在使用“斜率圖”光學元件時您可能無法獲得最有說服力的可視化效果。這很快就會變得復雜(很多點/線重疊)并且資訊丟失了。
要顯示成對觀察(治療 1 與治療 2)之間的變化,您還可以(我認為:更好)使用散點圖。您可以顯示每個觀察結果,變化立即變得清晰。為了使它更直觀,您可以添加一行等式。
我認為您不需要顯示估計的分布(左圖),但是如果您想顯示這一點,您可以使用帶有 geom_density2d(右圖)的二維密度估計
library(tidyverse)
## patchwork only for demo purpose
library(patchwork)
iris_edit <- iris %>% group_by(Species) %>%
## use seq_along instead
mutate(paired = seq_along(Species)) %>%
filter(Species %in% c("setosa","versicolor")) %>%
## some more modificiations
select(paired, Species, Sepal.Length) %>%
pivot_wider(names_from = Species, values_from = Sepal.Length)
lims <- c(0, 10)
p1 <-
ggplot(data = iris_edit, aes(setosa, versicolor))
geom_abline(intercept = 0, slope = 1, lty = 2)
geom_point(alpha = .7, stroke = 0, size = 2)
cowplot::theme_minimal_grid()
coord_equal(xlim = lims, ylim = lims)
labs(x = "Treatment 1", y = "Treatment 2")
p2 <-
ggplot(data = iris_edit, aes(setosa, versicolor))
geom_abline(intercept = 0, slope = 1, lty = 2)
geom_density2d(color = "Grey")
geom_point(alpha = .7, stroke = 0, size = 2)
cowplot::theme_minimal_grid()
coord_equal(xlim = lims, ylim = lims)
labs(x = "Treatment 1", y = "Treatment 2")
p1 p2

由reprex 包(v2.0.1)于 2021 年 12 月 18 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/387232.html
標籤:r ggplot2 violin-plot
