Re: [问题] 想画多组XY折线图,X数值和长度都不同

楼主: LIAR (玻璃做的大叔)   2021-06-02 23:55:10
: 推 andrew43: 建议先melt成 long table,一列一个观测,有一个病人多 05/26 22:56
: → andrew43: 列。 05/26 22:56
: → andrew43: 每一个病人多列。 05/26 22:57
: → andrew43: 之后ggplot,并facet_warp(vars(病人id)) 05/26 22:59
: → andrew43: facet可以指定scales 05/26 23:00
: 推 locka: 推A大,补充一下ggplot的哲学就是把资料整理成long table~ 05/27 03:24
: 推 locka: (是说如果病人id很多个,分组画出来的图会很挤很挤喔,慎用 05/27 03:24
: 推 locka: ) 05/27 03:24
抱歉我发现我没有讲清楚,我是要画在同一张图,不过根据facet的功能,比较像是
把许多小图组合成一张大图,像是论文的fig.1A, fig.1B, fig.1C那样,而我想要
的是:
https://imgur.com/a/zQxKbg9
这种的,
红线有到40天,绿线不到20天;红线20天前只有5~6点,蓝线超过10点以上。
然后要合再一起画图。
我当时电脑因为不明原因无法安装ggplot2,后来用plot + lines,用循环搞出
上面那张图出来,
plot(analysisArray[testGroup[i],1,], analysisArray[testGroup[i],Y+1,],
xlim = range(analysisArray[,1,],na.rm = TRUE),
ylim = range(analysisArray[,Y+1,],na.rm = TRUE),
xlab=names(MySheet)[repPos], ylab=names(MySheet)[repPos+Y],
pch=16, cex=1, type = "b",col=colors[i] , main=paste("手术组",
titleName[Y]))
for(i in 2:length(testGroup)){
lines(analysisArray[testGroup[i],1,], analysisArray[testGroup[i],Y+1,],
pch=16, cex=1, type = "b",col=colors[i])
}
另外有乡民网友提供ggplot2的解
ggplot() +
geom_line(data=p1, aes(x=dates, y=val, color="p1")) +
geom_point(data=p1, aes(dates,val), color="red") +
geom_line(data=p2, aes(x=dates, y=val, color="p2")) +
geom_point(data=p2, aes(dates,val), color="darkcyan") +
scale_x_datetime(breaks=unique(combine(dates1, dates2)),
date_labels = '%m/%d') +
theme(axis.text.x = element_text(size = 10,
vjust = 0.5,
hjust = 0.5,
angle = 90))
以上,回报目前的成果。谢谢各位的帮忙!
作者: cywhale (cywhale)   2021-06-03 10:58:00
基本上还是要把资料用成long table 不同颜色的组别资料就在资料上给予一个组别变量比如grp ggplot(aes(x=time,y=value,colour=grp), data=dt) 可以画出类似你的范例图

Links booklink

Contact Us: admin [ a t ] ucptt.com