我遇到了与in this post类似的问题,解决方案几乎对我的数据有效,但当我添加抖动时,我遇到了将点连接到线的问题。我将抖动添加到geom_point(position =“jitter”)分量,因为我的各个点在条形图中重叠,但是现在我的各条线都没有连接到点上。2有人对如何处理这个问题有什么建议吗?我已经尝试过创建抖动的帧值,但它也没有工作。
我尝试了下面的代码,希望将各个点连接到线上,但它不起作用。
我也尝试在x中添加交互,但也不起作用。
library(ggplot2)
df <- data.frame(
Group = c("A", "A", "A", "A", "B", "B"),
Subgroup = c("A.1", "A.2", "A.1", "A.2", "B.1", "B.2"),
Value = c(10, 7, 8, 9, 11, 12),
Pair = c(1, 1, 2, 2, 3, 3)
)
ggplot(df, aes(x = Subgroup, y = Value, fill = Subgroup)) +
geom_bar(stat = "summary", fun = "mean", width = 1) +
geom_point(position="jitter") +
geom_line(aes(group = Pair), color = "red", ) +
facet_wrap(vars(Group), scales = "free_x", strip.position = "bottom") +
labs(x = "Group") +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
strip.background = element_blank(),
panel.spacing = unit(0, units = "line")
)
字符串
在geom_line和geom_point中实现position = position_jitter(seed = 1)时,Edit =工作。但不幸的是,对于我的数据,它仍然不工作。
我的密码:
tdcs_cer <- as.data.frame(structure(list(Subject = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5,
5, 5, 5, 5, 5, 5, 13, 13, 13, 13, 13, 13, 13, 13, 13, 17, 17,
17, 17, 17, 17, 17, 17, 17, 21, 21, 21, 21, 21, 21, 21, 21, 21,
25, 25, 25, 25, 25, 25, 25, 25, 25, 29, 29, 29, 29, 29, 29, 29,
29, 29, 33, 33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37, 37,
37, 37, 37, 37, 41, 41, 41, 41, 41, 41, 41, 41, 41, 45, 45, 45,
45, 45, 45, 45, 45, 45, 49, 49, 49, 49, 49, 49, 49, 49, 49, 53,
53, 53, 53, 53, 53, 53, 53, 53, 57, 57, 57, 57, 57, 57, 57, 57,
57, 61, 61, 61, 61, 61, 61, 61, 61, 61, 65, 65, 65, 65, 65, 65,
65, 65, 65, 77, 77, 77, 77, 77, 77, 77, 77, 77, 81, 81, 81, 81,
81, 81, 81, 81, 81, 89, 89, 89, 89, 89, 89, 89, 89, 89), Group = c("cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum",
"cerebellum", "cerebellum", "cerebellum", "cerebellum", "cerebellum"
), Music = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0), StimType = structure(c(2L, 2L, 2L, 3L,
3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L,
2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L,
2L, 2L, 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2, 5, 12, 2, 20, 6, 1, 20, 4, 20, 20, 4, 1, 20,
2, 1, 20, 3, 1, 20, 20, 20, 1, 5, 2, 1, 9, 1, 1, 4, 1, 9, 19,
6, 2, 18, 1, 3, 14, 6, 1, 20, 20, 1, 5, 20, 2, 10, 20, 2, 13,
1, 2, 11, 2, 4, 15, 1, 7, 16, 5, 1, 8, 2, 6, 15, 7, 4, 14, 4,
4, 15, 2, 3, 12, 2, 1, 7, 1, 4, 14, 1, 1, 9, 2, 6, 4, 1, 20,
2, 20, 20, 20, 20), PropCorr = c(15, 40, 15, 5, 50, 0, 5, 15,
10, 15, 50, 10, 10, 20, 5, 10, 45, 5, 5, 50, 15, 15, 60, 10,
20, 75, 20, 45, 90, 30, 20, 55, 20, 30, 70, 15, 10, 35, 5, 5,
15, 10, 0, 35, 0, 30, 60, 5, 25, 55, 20, 35, 65, 5, 30, 80, 5,
25, 90, 15, 15, 70, 20, 5, 5, 0, 0, 0, 0, 10, 0, 0, 10, 35, 15,
10, 55, 10, 25, 60, 10, 0, 30, 5, 0, 20, 0, 0, 20, 5, 0, 10,
5, 0, 15, 5, 0, 0, 0, 5, 25, 10, 5, 45, 5, 5, 20, 5, 45, 95,
30, 10, 90, 5, 15, 70, 30, 5, 0, 0, 5, 25, 0, 10, 50, 0, 10,
65, 5, 10, 55, 10, 20, 75, 5, 35, 80, 25, 5, 40, 10, 30, 75,
35, 20, 70, 20, 20, 75, 10, 15, 60, 10, 5, 35, 5, 20, 70, 5,
5, 45, 10, 30, 20, 5, 0, 10, 0, 0, 0, 0)), row.names = c(NA,
-171L), class = c("tbl_df", "tbl", "data.frame")))
我的身材代码:
My_Theme = theme(
title = element_text(size = 15),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.title.y = element_text(size = 16),
axis.text.y = element_text(size = 15),
strip.text.y = element_blank(),
strip.text = element_text(size=15),
strip.background = element_blank(),
strip.placement = "outside",
axis.ticks.x = element_blank(),
axis.line.x = element_line(linetype="solid"),
legend.text = element_text(size = 13))
cer_plot <- ggplot(tdcs_cer, aes(x=StimType, y = PropCorr, fill = StimType)) +
geom_bar(stat = "summary", fun = "mean", width = 0.6, size=0.5, alpha=0.9) +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.3, size = 0.5, alpha = 0.4) +
geom_point(aes(x=StimType,y=PropCorr, color=StimType), size=2.5,position = position_jitter(seed = 1)) +
scale_color_manual(values=c("lightgrey", "pink", 'cyan'))+
geom_line(aes(group = Subject), color = "antiquewhite3", size=0.6, alpha=0.5, position = position_jitter(seed = 1)) +
facet_wrap(vars(condition), scales = "free_x", strip.position = "bottom") +
scale_fill_manual(values = c("Sham" = "darkgrey",
"Anodal" = "brown1",
"Cathodal" = "blue"))+
ggtitle("Right Cerebellum") +
labs(x = "", y = "Percent Correct")+
theme_classic()+
My_Theme
2条答案
按热度按时间vhmi4jdf1#
你必须在ggplot函数外定义jitter,然后在position参数中引用该对象。此外,你需要对所有使用jittering的层应用相同的分组。下面的代码在我这边工作:
字符串
0sgqnhkj2#
你必须在
geom_line
中使用position_jitter
,并通过seed=
参数设置相同的随机种子:字符串
EDIT您的真实的数据和代码的问题是
geom_point
和geom_line
的数据顺序不同(不幸的是,我不知道为什么)。因此,抖动点/我想出的唯一解决办法是在ggplot
之外手动抖动StimType
和PropCorr
,并使用抖动的两层的值。