如何在 x-axis 中以正确的格式绘制日期,以确保 r 汽车包中 follow-up 绘图的完整性?
How to plot date in a proper format in x-axis in the completeness of the follow-up plot in r car package?
我试图在 r
中绘制 follow-up 绘图的完整性,但我在以正确格式绘制日期时遇到 x-axis 问题,尽管我将其放入使用 POSIXct
的数据集中的正确格式
我的代码和示例数据集:
Date.Interventon = as.POSIXct('1/27/2017', format='%m/%d/%Y') + 1:40*60
Status<- c ("Alive","Dead","Dead","Dead","Alive","Alive","Dead","Dead","Alive","Dead")
OS.years <-c(11,13,14,13,13,10,13,14,10,11)
data.fu.lorenzoo= data.frame(Date.Interventon,Status, OS.years)
str(Date.Interventon )
library(car)
scatterplot( OS.years ~ Date.Interventon | Status, data=data.fu.lorenzoo,
xlab = "Date of procedure",
ylab = "Follow-up (years)",
smooth = F, # Removes smooth estimate
regLine = T) # Removes linear estimate
结果图如下所示。
我做了很多 trials 但我仍在挣扎。
任何建议将不胜感激。
可能不是最好的解决方案,您需要单独格式化轴。
Date.Interventon = as.POSIXct("1/27/2017", format="%m/%d/%y") + 1:40*60
Status<- c ("Alive","Dead","Dead","Dead","Alive","Alive","Dead","Dead","Alive","Dead")
OS.years <-c(11,13,14,13,13,10,13,14,10,11)
data.fu.lorenzoo= data.frame(Date.Interventon, Status, OS.years)
library(car)
scatterplot( OS.years ~ Date.Interventon| Status, data=data.fu.lorenzoo,
xlab = "Date of procedure",
ylab = "Follow-up (years)",
smooth = F, # Removes smooth estimate
regLine = T,
axes=F) # Removes linear estimate
axis(2)
axis(1, Date.Interventon, format( as.POSIXct("1/27/2017", format="%m/%d/%y") + 1:40*60, cex.axis = .7))
我不确定这是否是您所需要的,但是根据您的数据集,我假设您想要查看 x 轴上的分钟数,因为天数和小时数彼此相似。
使用 lubridate
包中的 minute
,您可以以适当的方式更改 x 轴。
car::scatterplot( OS.years ~ minute( Date.Interventon) | Status, data=data.fu.lorenzoo,
xlab = "Date of procedure",
ylab = "Follow-up (years)",
smooth = F, # Removes smooth estimate
regLine = T) # Removes linear estimate
通过使用不同的包,例如ggplot2
,我们可以产生相似的情节。
在继续之前,我更新了 Data.Inverventon 以代表不同的年份。然后使用 zoo
包中的 as.yearmon
函数创建了 yearmonth 变量。
Date.Interventon = as.POSIXct('1/27/2017', format='%m/%d/%Y') + 1:40*60000000
Status<- c ("Alive","Dead","Dead","Dead","Alive","Alive","Dead","Dead","Alive","Dead")
OS.years <-c(11,13,14,13,13,10,13,14,10,11)
data.fu.lorenzoo= data.frame(Date.Interventon,Status, OS.years)
data.fu.lorenzoo$yearmonth = zoo::as.yearmon(data.fu.lorenzoo$Date.Interventon)
然后你可以画出类似的图
ggplot(data.fu.lorenzoo, aes(x = yearmonth, y = OS.years, color = Status)) +
geom_point() + geom_smooth(method = lm, se = FALSE) +
labs( x = "Date of procedure", y = "Follow-up (years)") +
theme_minimal()
输出就是这样,
我试图在 r
中绘制 follow-up 绘图的完整性,但我在以正确格式绘制日期时遇到 x-axis 问题,尽管我将其放入使用 POSIXct
我的代码和示例数据集:
Date.Interventon = as.POSIXct('1/27/2017', format='%m/%d/%Y') + 1:40*60
Status<- c ("Alive","Dead","Dead","Dead","Alive","Alive","Dead","Dead","Alive","Dead")
OS.years <-c(11,13,14,13,13,10,13,14,10,11)
data.fu.lorenzoo= data.frame(Date.Interventon,Status, OS.years)
str(Date.Interventon )
library(car)
scatterplot( OS.years ~ Date.Interventon | Status, data=data.fu.lorenzoo,
xlab = "Date of procedure",
ylab = "Follow-up (years)",
smooth = F, # Removes smooth estimate
regLine = T) # Removes linear estimate
结果图如下所示。
我做了很多 trials 但我仍在挣扎。
任何建议将不胜感激。
可能不是最好的解决方案,您需要单独格式化轴。
Date.Interventon = as.POSIXct("1/27/2017", format="%m/%d/%y") + 1:40*60
Status<- c ("Alive","Dead","Dead","Dead","Alive","Alive","Dead","Dead","Alive","Dead")
OS.years <-c(11,13,14,13,13,10,13,14,10,11)
data.fu.lorenzoo= data.frame(Date.Interventon, Status, OS.years)
library(car)
scatterplot( OS.years ~ Date.Interventon| Status, data=data.fu.lorenzoo,
xlab = "Date of procedure",
ylab = "Follow-up (years)",
smooth = F, # Removes smooth estimate
regLine = T,
axes=F) # Removes linear estimate
axis(2)
axis(1, Date.Interventon, format( as.POSIXct("1/27/2017", format="%m/%d/%y") + 1:40*60, cex.axis = .7))
我不确定这是否是您所需要的,但是根据您的数据集,我假设您想要查看 x 轴上的分钟数,因为天数和小时数彼此相似。
使用 lubridate
包中的 minute
,您可以以适当的方式更改 x 轴。
car::scatterplot( OS.years ~ minute( Date.Interventon) | Status, data=data.fu.lorenzoo,
xlab = "Date of procedure",
ylab = "Follow-up (years)",
smooth = F, # Removes smooth estimate
regLine = T) # Removes linear estimate
通过使用不同的包,例如ggplot2
,我们可以产生相似的情节。
在继续之前,我更新了 Data.Inverventon 以代表不同的年份。然后使用 zoo
包中的 as.yearmon
函数创建了 yearmonth 变量。
Date.Interventon = as.POSIXct('1/27/2017', format='%m/%d/%Y') + 1:40*60000000
Status<- c ("Alive","Dead","Dead","Dead","Alive","Alive","Dead","Dead","Alive","Dead")
OS.years <-c(11,13,14,13,13,10,13,14,10,11)
data.fu.lorenzoo= data.frame(Date.Interventon,Status, OS.years)
data.fu.lorenzoo$yearmonth = zoo::as.yearmon(data.fu.lorenzoo$Date.Interventon)
然后你可以画出类似的图
ggplot(data.fu.lorenzoo, aes(x = yearmonth, y = OS.years, color = Status)) +
geom_point() + geom_smooth(method = lm, se = FALSE) +
labs( x = "Date of procedure", y = "Follow-up (years)") +
theme_minimal()
输出就是这样,