我遇到了与in this post 类似的问题,这个解决方案几乎对我的数据有效,但当我添加抖动时,我在将点连接到线上时遇到了问题.我将抖动添加到geom_point(point="jitter")组件,因为我的各个点在条形图中重叠,但现在我的各个线没有连接到这些点.有没有人对如何处理这个问题有什么建议?我已经try 在数据帧的值中创建抖动,但也不起作用.

我try 了下面的代码,希望将各个点连接到线上,但它不起作用.

我也try 在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中实现位置=POSITE_JITER(种子=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,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 20, 7, 20, 6, 12, 1, 5, 11, 4, 7, 13, 1, 
6, 16, 1, 5, 18, 3, 3, 14, 4, 1, 1, 20, 20, 20, 20, 2, 20, 20, 
2, 7, 3, 2, 11, 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

推荐答案

您还必须在geom_line中使用position_jitter,并通过seed=参数设置相同的随机种子:

library(ggplot2)

ggplot(df, aes(x = Subgroup, y = Value, fill = Subgroup)) +
  geom_bar(stat = "summary", fun = "mean", width = 1) +
  geom_point(position = position_jitter(seed = 1)) +
  geom_line(aes(group = Pair),
    color = "red",
    position = position_jitter(seed = 1)
  ) +
  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")
  )

EDIT您的真实数据和代码的问题是,geom_pointgeom_line的数据顺序不同(不幸的是,我不知道为什么).因此,抖动的点/线不会对齐.我想出来解决这个问题的唯一 Select 是手动抖动ggplot以外的抖动StimTypePropCorr,并对这两个层使用抖动值.

library(ggplot2)

tdcs_cer$StimType1 <- jitter(
  as.numeric(factor(tdcs_cer$StimType)),
  amount = .45
)

tdcs_cer$PropCorr1 <- jitter(
  tdcs_cer$PropCorr,
  amount = .45
)

ggplot(tdcs_cer, aes(x = StimType, y = PropCorr)) +
  geom_bar(
    aes(fill = StimType),
    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 = StimType1,
      y = PropCorr1,
      color = StimType
    ),
    size = 2.5
  ) +
  geom_line(
    aes(
      x = StimType1,
      y = PropCorr1,
      group = Subject
    ),
    color = "antiquewhite3",
    size = 0.6, alpha = 0.5
  ) +
  facet_wrap(vars(condition),
    scales = "free_x",
    strip.position = "bottom"
  ) +
  scale_fill_manual(values = c(
    "Sham" = "darkgrey",
    "Anodal" = "brown1",
    "Cathodal" = "blue"
  )) +
  scale_color_manual(
    values = c("lightgrey", "pink", "cyan")
  ) +
  ggtitle("Right Cerebellum") +
  labs(x = "", y = "Percent Correct") +
  theme_classic() +
  My_Theme

enter image description here

R相关问答推荐

棒棒糖图表大小和线宽参数故障标签未出现

按块将载体转换为矩阵-reshape

如何根据包含相同值的某些列获取总额

如何在四进制仪表板值框中显示值(使用shiny 的服务器计算)

从多个前置日期中获取最长日期

x[[1]]中的错误:脚注越界

如何根据条件计算时差(天)

从开始时间和结束时间导出时间

格点中指数、双曲和反双曲模型曲线的正确绘制

如果某些列全部为NA,则更改列

使用整齐的计算(curl -curl )和杂音

找出二叉树中每个 node 在R中的深度?

在另一个包中设置断点&S R函数

如何平滑或忽略R中变量的微小变化?

在R中,如何从一系列具有索引名的变量快速创建数据帧?

在具有多个响应变量的比例堆叠条形图上方添加总计

在r中整理图例和堆叠图的问题

TidyVerse中长度不等的列结合向量

名字的模糊匹配

汇总数据:在跨越()all_of()Dynamic_list_of_vars=>;所选内容不能有缺失值的汇总()中出错