必须有很多方法可以使用间隔删失数据制作后续时间图,尽管快速的谷歌搜索只在审查概览中找到了这张图片,这在我看来有点忙。
只是为了给出另一个观点,这是一种使用 ggplot2 包的方法。
require(ggplot2)
# Your example data
dat <- structure(list(ID = 1:5, eventA = c(0L, 1L, 1L, 0L, 1L),
eventB = c(1L, 0L, 0L, 1L, 0L), t1 = c(7, 5, 10, 4.5, 2), t2 = c(7, 5, 10, 4.5,
8), censored = c(0, 0, 0, 0, 1)), .Names = c("ID", "eventA",
"eventB", "t1", "t2", "censored"), class = "data.frame", row.names = c(NA, -5L))
# Create event variable
dat$event <- with(dat, ifelse(eventA, "A", "B"))
# Create id.ordered, which is a factor that is ordered by t2
# This will allow the plot to be ordered by increasing t2, if desired
dat$id.ordered <- factor(x = dat$ID, levels = order(dat$t2, decreasing = T))
# Use ggplot to plot data from dat object
ggplot(dat, aes(x = id.ordered)) +
# Plot solid line representing non-interval censored time from 0 to t1
geom_linerange(aes(ymin = 0, ymax = t1)) +
# Plot line (dotted for censored time) representing time from t1 to t2
geom_linerange(aes(ymin = t1, ymax = t2, linetype = as.factor(censored))) +
# Plot points representing event
# The ifelse() function moves censored marker to middle of interval
geom_point(aes(y = ifelse(censored, t1 + (t2 - t1) / 2, t2), shape = event),
size = 4) +
# Flip coordinates
coord_flip() +
# Add custom name to linetype scale,
# otherwise it will default to "as.factor(censored))"
scale_linetype_manual(name = "Censoring", values = c(1, 2),
labels = c("Not censored", "Interval censored")) +
# Add custom shape scale. Change the values to get different shapes.
scale_shape_manual(name = "Event", values = c(19, 15)) +
# Add main title and axis labels
opts(title = "Patient follow-up") + xlab("Patient ID") + ylab("Days") +
# I think the bw theme looks better for this graph,
# but leave it out if you prefer the default theme
theme_bw()
结果:

在根据数据制作具有线型、颜色、大小等的图形时,我发现 ggplot2 比基本图形甚至格状图更直观,尽管格状图在绘制更大的数据时要快得多。
我不确定是否最好将事件标记放置在审查间隔的中间或末尾。我这里选择了中间,强调事件不一定发生在接近尾声的时候。
附录
一旦您决定了一个标准绘图,并且如果您发现自己经常制作这些绘图,则可以方便地将其包装在一个函数中并使用 R 的 S3 对象系统通过调用plot()泛型来调度绘图方法。以下内容与原始问题没有直接关系,但为了其他有兴趣向plot()泛型添加方法的读者,我将在此处包含它。首先,将 ggplot 调用封装到 plot 方法函数中:
plot.interval.censored <- function(x, title = "Patient follow-up",
xlab = "Patient ID", ylab = "Days",
linetype.values = c(1, 2), shape.values = c(19, 15))
{
x$event <- with(dat, ifelse(eventA, "A", "B"))
x$id.ordered <- factor(x = dat$ID, levels = order(dat$t2, decreasing = T))
out <- ggplot(x, aes(x = id.ordered)) +
geom_linerange(aes(ymin = 0, ymax = t1)) +
geom_linerange(aes(ymin = t1, ymax = t2, linetype = as.factor(censored))) +
geom_point(aes(y = ifelse(censored, t1 + (t2 - t1) / 2, t2), shape = event),
size = 4) +
coord_flip() +
scale_linetype_manual(name = "Censoring", values = linetype.values,
labels = c("Not censored", "Interval censored")) +
scale_shape_manual(name = "Event", values = shape.values) +
opts(title = title) + xlab(xlab) + ylab(ylab) +
theme_bw()
return(out)
}
然后,添加internal.censored到数据对象的类中:
class(dat) <- c("interval.censored", class(dat))
现在,只需调用以下命令即可生成该图:
plot(dat)
您可以将其plot.interval.censored()放入一个包中,也可以将其添加到您的 .Rprofile 中,在这种情况下,当您在计算机上启动 R 时,它始终可供您使用。发布包可能是首选,因为当您不在自己的计算机上时,它更容易与他人共享或安装。但是,编辑 .Rprofile 可能更简单。Hadley 对 S3 对象系统有一个很好的概述。