在處理時序性變量時,通常需要比較前後變化趨勢,這個時候需要將匹配的變量連線起來,從而可以直觀地展示個體數據的變化。
如何繪製連線,有兩種方法。
第一種: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