![](/img/trans.png)
[英]Boxplot with ggplot2: Trying to lay geom_jitter over code for plot, strange outlier dots
[英]R ggplot2 : geom_jitter and fill, problem to have the dots on the right boxplot
这是我的 R 代码
ggplot(dat = Table, aes(x = Group, y = value, fill = Type)) +
geom_boxplot(alpha=0.08)+
geom_jitter()+
scale_fill_brewer(palette="Spectral")+
theme_minimal()
就像您看到的那样,这些点位于箱线图的中间。 我可以在 geom_jitter 中添加什么,让每个点都在右箱图中,而不是像这样在中间? 我也试过 geom_point,它给出了相同的结果!
多亏了现在的帮助,它可以工作,但是我想添加一条线来连接点,我明白了……有人能告诉我如何真正将点与线连接起来吗
我认为如果你按interaction(Group, Type)
并使用position_jitterdodge()
你应该得到你正在寻找的东西。
ggplot(mtcars, aes(as.character(am), mpg, color = as.character(vs),
group = interaction(as.character(vs), as.character(am)))) +
geom_boxplot() +
geom_jitter(position = position_jitterdodge()) # same output with geom_point()
编辑 - 这是一个手动抖动应用于数据的示例,其中每个主题在每个组中出现一次。
我寻找了一种内置的方法来做到这一点, 这个答案很接近,但是我无法使用position_jitterdodge
和由 Group/Type 组定义的 position,但由id
单独定义的行分组来工作而不是按组/类型。 两种美学(位置调整和系列识别)都依赖于相同的group
参数,但它们各自需要不同的值。
Table = data.frame(id = 1:4,
value = rnorm(8),
Group = rep(c("a","b"), each = 4),
Type = c("1", "2"))
library(dplyr)
Table %>%
mutate(x = as.numeric(as.factor(Group)) +
0.2 * scale(as.numeric(as.factor(Type))) +
rnorm(n(), sd = 0.06)) %>%
ggplot(aes(x = Group, y = value, fill = Type, group = interaction(Group, Type))) +
geom_boxplot(alpha=0.2)+
geom_point(aes(x = x)) +
geom_line(aes(x = x, group = id), alpha = 0.1) +
scale_fill_brewer(palette="Spectral")+
theme_minimal()
如果你想让他们排队,最好使用 position_dodge 代替:
library(ggplot2)
Table <- tibble::tibble(
Group = rep(c("A", "B"), each = 20),
Type = factor(rep(c(1:2, 1:2), each = 10)),
value = rnorm(40, mean = 10)
)
ggplot(dat = Table, aes(x = Group, y = value, fill = Type)) +
geom_boxplot(alpha=0.08)+
geom_point(position = position_dodge(width = 0.75))+
scale_fill_brewer(palette="Spectral")+
theme_minimal()
要添加一条线,请确保group = ID
进入geom_point
和geom_line
调用:
library(ggplot2)
Table <- tibble::tibble(
Group = rep(c("A", "B"), each = 20),
Type = factor(rep(c(1:2, 1:2), each = 10)),
ID = factor(rep(1:20, times = 2)),
value = rnorm(40, mean = 10)
)
ggplot(dat = Table, aes(x = Group, y = value, fill = Type)) +
geom_boxplot(alpha = 0.08) +
geom_point(aes(group = ID), position = position_dodge(width = 0.75))+
geom_line(aes(group = ID), position = position_dodge(width = 0.75), colour = "grey")+
scale_fill_brewer(palette = "Spectral") +
theme_minimal()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.