关于双分组(时间点+分组)折线图绘制(带散点,带mean+sd,带显著性)

ggplot(food, aes(x=Days, y=Food,fill=Groups,color=Groups,shape=Groups))+
  geom_linerange(aes(ymin=Food, ymax=Food+sd),size=1.1,show.legend = F)+ #画从mean到上bar的线
  geom_errorbar(aes(ymin=Food+sd, ymax=Food+sd), width=2,size=1.1,show.legend = F)+ #画“上bar”
  geom_line(size=1.1,show.legend = F)+ #连接各平均值
  geom_point(size=4,alpha=0.8)+ #绘制平均值点
  geom_jitter(size=1.1,alpha=0.8,position = position_jitterdodge(),data = intake,show.legend = F)+ #加载原始数据集绘制散点图
  annotate(geom = "text",x=c(7,14,21,28,35,42),y=c(3.46,3.45,3.41,3.44,3.33,3.13),label=c("**","**","**","**","","**"),size=7)+ #加显著性,此处已经额外计算好
  scale_x_continuous(breaks = c(7,14,21,28,35,42))

此种图是先计算再画图,好处是对理解ggplot运行原理很有帮助,坏处是加显著性比较困难,且再添加散点图还要重新加载原始数据集,较为繁琐
下面介绍更好使的,ggpubr包中的ggline

ggline(intake, x="Days", y="Food", plot_type = "l",size = 1.1,
       add = c("mean_sd","jitter"),error.plot = "upper_errorbar",
       color = "Groups",shape = "Groups",fill="Groups",
       add.params=list(color = "Groups",shape = "Groups",fill="Groups"))+
  stat_compare_means(aes(group=Groups), label = "p.signif",label.y = 3.5,size=7,
                     symnum.args = list(cutpoints = c(0,0.0001,0.001,0.01,0.05,1),
                                        symbols = c("****","***","**","*", "")))+
  scale_x_discrete(breaks = c(7,14,21,28,35,42))#此处与前面不一,一个连续,一个离散

这个是出图方便,不需重新加载数据集,坏处是平均值附近的横线(也就是下“bar”)去不掉,影响美观,同时对于连续不等间距分隔的数据不甚友好,其会始终等间距分割,还有还有,无法为每一个星号定义一个位置y
最新发现:当一个时间点有多个组别时完全gg,只能显示一个p值,是kruskal.test或anova的全局数值

具体爱用那个就仁者见仁智者见智了

你可能感兴趣的:(关于双分组(时间点+分组)折线图绘制(带散点,带mean+sd,带显著性))