所用R包: tidyverse
1.所需数据计算
#数据转置
adam1 <- adam %>% pivot_longer(variables,names_to="Time",values_to="ADL")
#计算ADL的标准误
tgc1 <- summarySE(adam1, measurevar="ADL", groupvars=c("分组","Time"),na.rm=T)
#组间差异检验
t.test(adam$ADL基线总分0,by="分组")
#或使用gtsummay包批量检验
2.绘图
P1 <- ggplot(tgc1, aes(x = Time, y = ADL, colour = 分组, group = 分组)) +
geom_errorbar(aes(ymin = ADL - se, ymax = ADL + se), width = 0.1) + #添加误差区间
geom_line() +
geom_point(shape = 16, size = 3) + # Use shape = 17 for triangles and adjust size
annotate(geom="text",x=5,y=-5.9,label="*")+ #在有差异的时点添加标识符
theme_bw() +
labs(
x = "Week", # Set the x-axis label
y = "ADL Score", # Set the y-axis label
colour = "Group" # Set the legend title
) +
geom_hline(yintercept = 0, linetype = "dashed", colour = "black")+ #添加水平虚线
scale_colour_manual(
values = c("red", "blue"), # Define custom colours for groups
labels = c("组1", "组2") # Set the labels for groups
) +
theme(
legend.position = "top", # Position the legend at the top
legend.title = element_text(size = 12), # Adjust legend title font size
legend.text = element_text(size = 10), # Adjust legend text size
axis.title = element_text(size = 14), # Adjust axis title font size
axis.text = element_text(size = 12) # Adjust axis text font size
)
P1