简体   繁体   English

如何在 R 中的 plot 上连接数据点(针对每个主题)?

[英]How to connect data points (for each subject) on a plot in R?

I'm plotting each subject's proportion of responses to "same" or "different" stimuli across two blocks (A block and B block), meaning that each subject has 4 data points plotted.我正在绘制每个受试者对两个块(A 块和 B 块)的“相同”或“不同”刺激的反应比例,这意味着每个受试者都绘制了 4 个数据点。 Two for A block (same and different) and two for B block (same and different).两个用于 A 块(相同和不同),两个用于 B 块(相同和不同)。 Also means there are 4 rows of data containing proportion of responses for each subject.也意味着有 4 行数据包含每个主题的响应比例。

I would like to connect each subject's "same" and "different" data points with a line WITHIN a block (eg their one datapoint for "same" and datapoint for "different" are connected via a line within the A block and within the B block for each subject).我想将每个主题的“相同”和“不同”数据点与块内的一条线连接起来(例如,它们的一个“相同”数据点和“不同”数据点通过 A 块内和 B 内的一条线连接每个主题的块)。

I tried using the "group = subject" that comes with geom_line(), but it connects ALL of the subject's data points when I would like to only connect data points within each block.我尝试使用 geom_line() 附带的“group = subject”,但是当我只想连接每个块内的数据点时,它会连接所有主题的数据点。

testplot <- ggplot(df, aes(x=block, y=prop, shape=con, colour=con)) +
  geom_point(position=position_dodge(width=.1)) +
  xlab("Block") + ylab("Prop of responses") +
  theme_bw(base_size = 13) +
  theme(legend.position="top")
plot(testplot)

sample dataset:样本数据集:

subj = c(1, 1, 2, 2, 3, 3, 4, 4, 1, 1, 2, 2, 3, 3, 4, 4)
prop = c(0.5, 0.8, 0.3, 0.7, 0.9, 0.4, 0.1, 0.5, 1, 0.5, 0.9, 0.2, 0.7, 0.4, 0.8, 0.1)
con = c("same", "same", "same", "same", "same", "same", "same", "same", "diff", "diff", "diff", "diff", "diff", "diff", "diff", "diff")
block = c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B")

df = data.frame(subj, prop, con, block)

I hope this makes sense, is this feasible?我希望这是有道理的,这可行吗? Any help is appreciated!任何帮助表示赞赏!

I think the best approach is with a facet_grid :我认为最好的方法是使用facet_grid

ggplot(df, aes(x=con, y=prop, shape=con)) +
  geom_point(aes(color=con), position=position_dodge(width=.1)) +
  geom_line(aes(group = subj)) +
  facet_grid(.~block) +
  xlab("Block") + ylab("Prop of responses") +
  theme_bw(base_size = 13) +
  theme(legend.position="top")

在此处输入图像描述

From looking at the other answers here, I perhaps misinterpreted, but I think this is possibly a clearer representation anyway:通过查看此处的其他答案,我可能误解了,但我认为这可能是一个更清晰的表示:

ggplot(df, aes(factor(subj), prop)) +
  geom_point(aes(fill = con), size = 3, shape = 21) +
  geom_line(aes(group = factor(subj))) +
  facet_grid(.~block, switch = "x") +
  scale_x_discrete(expand = c(2, 2)) +
  labs(x = "Subjects within block") +
  theme_classic() +
  theme(text = element_text(size = 12),
        strip.placement = "outside",
        strip.background = element_blank(),
        panel.grid.major.y = element_line(colour = "gray90"))

在此处输入图像描述

One approach to achieve your desired plot would be to use facet_wrap by block , map con on x and subj on group .实现所需 plot 的一种方法是使用facet_wrap by block , map con on xsubj on group Try this:尝试这个:

subj = c(1, 1, 2, 2, 3, 3, 4, 4, 1, 1, 2, 2, 3, 3, 4, 4)
prop = c(0.5, 0.8, 0.3, 0.7, 0.9, 0.4, 0.1, 0.5, 1, 0.5, 0.9, 0.2, 0.7, 0.4, 0.8, 0.1)
con = c("same", "same", "same", "same", "same", "same", "same", "same", "diff", "diff", "diff", "diff", "diff", "diff", "diff", "diff")
block = c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B")

df = data.frame(subj, prop, con, block)

library(ggplot2)

testplot <- ggplot(df, aes(x=con, y=prop, group = subj)) +
  geom_point(aes(shape=con, colour=con)) +
  geom_line() +
  xlab("Block") + ylab("Prop of responses") +
  theme_bw(base_size = 13) +
  theme(legend.position="top") +
  facet_wrap(~block, strip.position = "bottom") +
  theme(strip.placement = "outside", axis.text.x = element_blank(), axis.ticks.x = element_blank())
testplot

This answer is very similar to the one by Ian Cambell but with colour and shape used to represent the subject since the same/diff is shown on the x-axis and is therefore redundant in the legend.这个答案与 Ian Cambell 的答案非常相似,但使用颜色和形状来表示主题,因为 x 轴上显示了相同/差异,因此在图例中是多余的。 With the subjects easily identifiable, the plot is also similar to the one by Allan Cameron.由于对象易于识别,plot 也与 Allan Cameron 的相似。 Just to show another perspective.只是为了展示另一个视角。

ggplot(df, aes(x=con, y=prop, col=subj, shape=subj)) +
  geom_point() +
  geom_line(aes(group = subj)) +
  facet_grid(.~block) +
  xlab("Block") + ylab("Prop of responses") +
  theme_bw(base_size = 13) +
  theme(legend.position="top")

在此处输入图像描述

We can then compare the four subjects across the blocks (facets).然后,我们可以跨块(方面)比较四个主题。

These are all good suggestions and interesting ideas if you want to reconsider your data representation (facets in particular), but to actually answer your question, you want to paste the groups together, as in geom_line(aes(group=paste(subj, con)))如果您想重新考虑数据表示(特别是方面),这些都是很好的建议和有趣的想法,但要真正回答您的问题,您希望将组粘贴在一起,如geom_line(aes(group=paste(subj, con)))

ggplot(df, aes(x=block, y=prop, shape=con, colour=con)) +
  geom_point() +
  geom_line(aes(group=paste(subj, con))) +
  xlab("Block") + ylab("Prop of responses") +
  theme_bw(base_size = 13) +
  theme(legend.position="top")

结果图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM