在处理时序性变量时,通常需要比较前后变化趋势,这个时候需要将匹配的变量连线起来,从而可以直观地展示个体数据的变化。
如何绘制连线,有两种方法。
第一种:ggpubr包中的ggpaired函数
library(ggpubr)
before <-c(200.1, 190.9, 192.7, 213, 241.4, 196.9, 172.2, 185.5, 205.2, 193.7)
after <-c(392.9, 393.2, 345.1, 393, 434, 427.9, 422, 383.9, 392.3, 352.2)
d <- data.frame(before = before, after = after)
ggpaired(d, cond1 = "before", cond2 = "after",
fill = "condition", palette = "jco")
ggpaired(ToothGrowth, x = "supp", y = "len",
color = "supp", line.color = "gray", line.size = 0.4, palette = "npg")
第二种方法,直接用ggplot中的geom_line函数绘制( 数据为我自己数据,画的花里胡哨了点)
ggplot(data=data,aes(x=Time,y=TG,color=Time))+
geom_violin(aes(fill=Time))+
geom_boxplot(alpha=1, outlier.size=0, size=0.9, width=0.6)+
geom_jitter( position=position_jitter(0.17), size=1, alpha=0.7)+
geom_line(aes(group=group) ,size=0.8,colour="#9C9C9C",linetype="dotted")+
labs(x="Groups", y="TG")+theme_bw()+
scale_y_continuous(limits=c(0.5, 5))+
scale_color_lancet()+mytheme