我需要外面的半小提琴,所以我需要把它們從它們的“x”位置散開。我想要內部的geom_points(稍微在'a'和'b'之間)。
這:
df1 <- data.frame(ID = 1:12,
var1 = c('a', 'b', 'b', 'a', 'b', 'b', 'a', 'b', 'b', 'a', 'b', 'a'),
var2 = c(16, 11, 14, 19, 14.5, 15, 13, 10, 21, 15, 17, 10))
ggplot(df1, aes(var1, var2))
geom_violinhalf(mapping = aes(fill = var2),flip = c(1, 3))
geom_boxplot(alpha = 1, width=0.1)
geom_point()
給了我以下,所有東西都堆疊在 x 軸上:

謝謝。
uj5u.com熱心網友回復:
一種選擇是利用position_nudge移動幾何圖層的 x 位置。
但是,當您想要將類別的圖層向相反方向移動時,您必須按類別拆分資料集,并為每個類別分別拆分圖層。為此,我使用了輔助繪圖功能并purrr::map遍歷拆分的資料框:
library(ggplot2)
library(see)
ggplot(mapping = aes(var1, var2))
purrr::imap(split(df1, df1$var1), function(x, y) {
# Shift in which direction?
fac <- if (y == "a") 1 else -1
# Flip half violin in which direction?
flip <- y == "a"
list(
geom_violinhalf(data = x, mapping = aes(fill = var2), flip = flip, position = position_nudge(x = -.25 * fac)),
geom_boxplot(data = x, alpha = 1, width=0.1),
geom_point(data = x, position = position_nudge(x = .25 * fac))
)
})
scale_x_discrete(expand = c(0, .8))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416766.html
標籤:
