[英]How to plot a line connecting geom_points for repeated measures in R?
I'm looking for a way to connect my individual datapoints in my ggplot, so show that the data is a repeated measure of the same individual over time.我正在寻找一种方法来连接我的 ggplot 中的各个数据点,以便表明数据是随着时间的推移对同一个人的重复测量。 Until now, I've managed to create a barplot with the individual geom_point (datapoint per subject) on top.
到目前为止,我已经成功地创建了一个条形图,上面有单独的 geom_point(每个主题的数据点)。 I would however like to connect the dots matching to the same participant between the three timepoints.
但是,我想将三个时间点之间与同一参与者匹配的点连接起来。 Any pointers?
任何指针?
## Example data, data from two groups: patients and controls
data_ex <- data.frame( pnum = c(1,2,3,4,5,6,7,8,9,10),
group = c("patient", "patient","patient","patient","patient","control","control","control", "control", "control"),
age = c(24,35,43,34,55,24,36,43,34,54),
panas_1.1 = c(-26, -15, -17, -15, -20, -21, -18, -19, -16, -20),
panas_1.2 = c(-25, -19, -14, -18, -20, -22, -17, -19, -18, -19),
panas_1.3 = c(-22, -21, -18, -14, -21, -21, -14, -17, -16, -18))
## Reshape the data
data_ex_long <- data_ex %>% gather(key = time, value = PANAS_score, panas_1.1, panas_1.2, panas_1.3)
## plot the data
ggplot(data=data_ex_long,aes(x = time, y = PANAS_score, fill = group)) +
geom_bar(stat = "summary",fun.y = 'mean', colour="black", size=1.8, position = position_dodge(width = 1)) +
geom_point(aes(time, fill = group), colour="black", size = 3, shape = 21, position =
position_jitterdodge(jitter.width = 0.2, jitter.height=0.4,
dodge.width=0.9), alpha = 0.8) +
geom_errorbar(aes(size=2),stat = 'summary', position = 'dodge', color = "black", width = 1, size = 1, fatten = 2) +
theme(text = element_text(size = 18),
legend.position = "none",
axis.text.x = element_text(size=15, color="#000000"),
axis.text.y = element_text(size=20, color="#000000"),
axis.title.x = element_blank(),
axis.title.y = element_text(size=18, color="#000000"),
axis.line.x = element_line(colour = "black", size = 1.3),
axis.line.y = element_line(colour = "black", size = 1.3),
panel.border = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(),
panel.background = element_blank(),
axis.ticks.length = unit(.25, "cm"),
axis.line = element_line()) +
ylab(" PANAS score ") +
NULL
You can try你可以试试
pd <- position_dodge(0.8)
ggplot(data=data_ex_long,aes(x = time, y = PANAS_score, fill = factor(group, levels = c("patient", "control")))) +
geom_bar(stat = "summary",fun.y = 'mean', color=1, position = "dodge") +
geom_point(aes(group=pnum),position = pd, shape =21) +
geom_line(aes(group=pnum),position = pd) +
geom_errorbar(stat = 'summary', position = "dodge") +
scale_fill_discrete("")
According to your comment I recommend to switch completely to stat_summary
根据您的评论,我建议完全切换到
stat_summary
pd <- position_dodge(.9) pd <- position_dodge(.9)
data_ex_long$group <- factor(data_ex_long$group, levels = c("patient", "control"))
ggplot(data=data_ex_long,aes(x = time, y = PANAS_score, fill = group)) +
stat_summary(fun.y = "mean", geom = "bar",position = pd) +
stat_summary(aes(group = group),
fun.y = "mean", geom = "line",position = pd, size= 1.5 ) +
stat_summary(fun.data = mean_se, geom = "errorbar",position = pd, width = 0.3) +
geom_point(aes(group=pnum),position = pd, shape =21)
Used ggplot2_3.1.0
使用
ggplot2_3.1.0
I'm trying to do a similar plot but struggling! 我正在尝试做类似的情节,但正在努力! I have two repeated measures factors rather than a repeated measures and independent groups.
我有两个重复测量因素,而不是一个重复测量和独立的小组。 So on my x-axis I have a before and after drug measure, with two boxplots for before (where colour is different for resting vs action) and two boxplot for after (resting vs action in different colours).
因此,在我的x轴上,我有一个之前和之后的药物测量值,前两个框图(静止与动作的颜色不同),后两个框图(不同颜色的静止与动作的坐标)。 I want to be able to show the trend per subject, where there is a data point for each of the 4 four plots.
我希望能够显示每个主题的趋势,四个4个图中每个图都有一个数据点。
The below gives me box plots with jitter-ed points per box plot, but I want a line connecting the points for individual subjects. 以下是箱形图,每个箱形图带有抖动点,但是我想用一条线连接各个主题的点。
p <- ggplot(df, aes(x=Group, y=PLV, fill=Move)) +
geom_boxplot(outlier.size = -1) +
geom_point(df,mapping=aes(x=Group, y=PLV, fill=Move), position = position_jitterdodge 0.2)
df: df:
1 Before Rest 0.22238
2 Before Rest 0.21670
3 Before Rest 0.21443
4 Before Rest 0.22624
5 Before Move 0.22796
6 Before Move 0.22217
7 Before Move 0.21418
8 Before Move 0.21679
9 After Rest 0.36057
10 After Rest 0.26613
11 After Rest 0.27747
12 After Rest 0.32213
13 After Move 0.35226
14 After Move 0.27122
15 After Move 0.26650
16 After Move 0.28785
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.