如何使用R中的线功能逐个连接线图点?

时间:2017-06-29 15:37:27

标签: r graph line

我的Excel数据是这样的:

Date        Total Bilirubin(umol/l)
10/18/2015  336.9
10/21/2015  352.7
10/24/2015  330.4
10/27/2015  310.2
10/30/2015  390.3
11/2/2015   378.3
11/5/2015   435.8
11/8/2015   449.3
11/11/2015  460.7
11/14/2015  385.3
11/17/2015  350.7
11/20/2015  278.6
11/23/2015  252.1
11/26/2015  180.8
11/29/2015  159.4
12/2/2015   105.5
12/5/2015   83.2
12/8/2015   64.4
12/11/2015  60.8

我用R画这样的折线图:

dat<-read.xlsx("/Users/user/Research/Obstructive Jaundice/liuxuebin bilirubin.xlsx",sheetName = "liuxuebin_bilirubin")
dat$Date<-factor(dat$Date)

lines(dat$Date,dat$Total.Bilirubin.umol.l.,type="b",lwd=1.5,lty=3,col="red",pch=16)

我得到的图像是这样的: enter image description here

我想逐一连接点,看看胆红素的变化趋势。
我该怎么做?

1 个答案:

答案 0 :(得分:0)

我相信您的问题是将Date列导入为因子变量。如果是这种情况,则将列转换为字符对象,然后转换为Date对象。那么绘图功能应该可以正常工作。

dat<-read.table(text="Date        Total.Bilirubin(umol/l)
10/18/2015  336.9
                 10/21/2015  352.7
                 10/24/2015  330.4
                 10/27/2015  310.2
                 10/30/2015  390.3
                 11/2/2015   378.3
                 11/5/2015   435.8
                 11/8/2015   449.3
                 11/11/2015  460.7
                 11/14/2015  385.3
                 11/17/2015  350.7
                 11/20/2015  278.6
                 11/23/2015  252.1
                 11/26/2015  180.8
                 11/29/2015  159.4
                 12/2/2015   105.5
                 12/5/2015   83.2
                 12/8/2015   64.4
                 12/11/2015  60.8", header=TRUE)

#convert dat$Date from a factor to a Date object
dat$Date<-as.Date(as.character(dat$Date), format="%m/%d/%Y") 
#plot image
plot(dat$Date,dat$Total.Bilirubin.umol.l.,type="b",lwd=1.5,lty=3,col="red",pch=16)

enter image description here