【发布时间】:2022-01-12 05:02:10
【问题描述】:
我只是想绘制一个ggplot 在箱线图上覆盖点图的图。我得到了非常奇怪的结果,希望有人能告诉我为什么以及如何解决它。 Overlay geom_points() on geom_boxplot(fill=group)? 这是一个类似的问题。但我的关键问题是shape。
这里有一个例子:
library(ggplot2)
library(dplyr)
head(mtcars)
data = data.frame(
x = factor(mtcars$vs),
y = mtcars$wt,
fill = factor(mtcars$am)
) %>%
dplyr::arrange(x, fill) %>%
dplyr::mutate(shape = rep(letters[1:4], 8))
set.seed(1)
ggplot(data, aes(x, y, fill = fill)) +
geom_boxplot() +
geom_point(position=position_jitterdodge())
我可以得到一个情节:
然后我添加形状映射。您可以看到所有点都完全改变了。我想要的是与上面相同的情节,只是点的形状发生了变化。即,点的位置不应改变。不知道为什么添加形状映射后,点不正确地分配给框组。
set.seed(1)
ggplot(data, aes(x, y, fill = fill)) +
geom_boxplot() +
geom_point(aes(shape = shape), position=position_jitterdodge())
【问题讨论】:
-
geom_point(aes(shape = shape, group = fill), position=position_jitterdodge())的结果是否如您所愿? -
@JonSpring 是的!您能否发布一个答案,以便我接受作为解决方案?
标签: r ggplot2 plot shapes geom-point