【问题标题】:Multiple scatterplot figure in RR中的多个散点图
【发布时间】:2014-09-19 06:31:48
【问题描述】:

我有一个稍微复杂的绘图任务。我已经到了一半,很确定如何获得它。我有一个如下形式的数据集,有多个主题,每个主题都在 Treatgroup 0 或 Treatgroup 1 中,每个主题贡献几行数据。每行对应一个时间点,在该时间点“count1、count2、奇怪名称3 等列中有值。

任务 1。我需要计算每一行的“天数”,即访问日期 - 开始日期。我猜应该是一个应用类型函数。

任务 2。我必须为每个计数变量制作一个带有一个散点图的多图图(count1 的图,count2 的图等)。在每个散点图中,我需要根据“天数”(x 轴)绘制计数值(y 轴)并连接每个主题的点。治疗组 0 中的受试者是一种颜色,治疗组 1 中的受试者是另一种颜色。每个散点图应酌情用 count1、count2 等标记。

我正在尝试使用基本绘图功能,并采取了编写绘图功能以供以后调用的方法。我认为这可以工作,但需要一些语法帮助。

#Enter example data
tC <- textConnection("
ID  StartDate   VisitDate   Treatstarted    count1  count2  count3  Treatgroup
C0098   13-Jan-07   12-Feb-10   NA  457 343 957 0
C0098   13-Jan-06   2-Jul-10    NA  467 345 56  0
C0098   13-Jan-06   7-Oct-10    NA  420 234 435 0
C0098   13-Jan-05   3-Feb-11    NA  357 243 345 0
C0098   14-Jan-06   8-Jun-11    NA  209 567 254 0
C0098   13-Jan-06   9-Jul-11    NA  223 235 54  0
C0098   13-Jan-06   12-Oct-11   NA  309 245 642 0
C0110   13-Jan-06   23-Jun-10   30-Oct-10   629 2436    45  1
C0110   13-Jan-07   30-Sep-10   30-Oct-10   461 467 453 1
C0110   13-Jan-06   15-Feb-11   30-Oct-10   270 365 234 1
C0110   13-Jan-06   22-Jun-11   30-Oct-10   236 245 23  1
C0151   13-Jan-08   2-Feb-10    30-Oct-10   199 653 456 1
C0151   13-Jan-06   24-Mar-10   3-Apr-10    936 25  654 1
C0151   13-Jan-06   7-Jul-10    3-Apr-10    1147    254 666 1
C0151   13-Jan-06   9-Mar-11    3-Apr-10    1192    254 777 1
")
data1 <- read.table(header=TRUE, tC)
close.connection(tC)

# format date
data1$VisitDate <- with(data1,as.Date(VisitDate,format="%d-%b-%y"))

# stuck: need to define days as VisitDate - StartDate for each row of dataframe (I know I need an apply family fxn here)
data1$Days <- [applyfunction of some kind ](VisitDate,ID,function(x){x-data1$StartDate})))

# Unsure here. Need to define plot function
plot_one <- function(d){
 with(d, plot(Days, Count, t="n", tck=1, cex.main = 0.8, ylab = "", yaxt = 'n', xlab = "", xaxt="n",  xlim=c(0,1000), ylim=c(0,1200))) # set limits
    grid(lwd = 0.3, lty = 7)
    with(d[d$Treatgroup == 0,], points(Days, Count1, col = 1)) 
    with(d[d$Treatgroup == 1,], points(Days, Count1, col = 2))
}

#Create multiple plot figure
par(mfrow=c(2,2), oma = c(0.5,0.5,0.5,0.5), mar = c(0.5,0.5,0.5,0.5))
#trouble here. I need to call the column names somehow, with; plyr::d_ply(data1, ???, plot_one) 

【问题讨论】:

  • 不要将rm(list=ls()) 放在示例代码中,以免有人在提供帮助时破坏他们的工作数据。
  • 请注意:您可以将字符串直接传递给text 参数到read.table,而无需使用文本连接。

标签: r function plot


【解决方案1】:

任务 1:

data1$days <- floor(as.numeric(as.POSIXlt(data1$VisitDate,format="%d-%b-%y")
                              -as.POSIXlt(data1$StartDate,format="%d-%b-%y")))

任务 2:

par(mfrow=c(3,1), oma = c(2,0.5,1,0.5), mar = c(2,0.5,1,0.5))
plot(data1$days, data1$count1, col=as.factor(data1$Treatgroup), main="count1")
plot(data1$days, data1$count2, col=as.factor(data1$Treatgroup), main="count2")
plot(data1$days, data1$count3, col=as.factor(data1$Treatgroup), main="count3")

【讨论】:

  • 很好的解决方案,谢谢。完全按预期工作。使用此解决方案,是否可以为每个治疗组选择颜色?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 2016-04-07
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
相关资源
最近更新 更多