R中的多个散点图 [英] Multiple scatterplot figure in R

查看:53
本文介绍了R中的多个散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a slightly complicated plotting task. I am half way there, quite sure how to get it. I have a dataset of the form below, with multiple subjects, each in either Treatgroup 0 or Treatgroup 1, each subject contributing several rows of data. Each row corresponds to a single timepoint at which there are values in columns "count1, count2, weirdname3, etc.

任务 1. 我需要计算每一行的天数",它只是访问日期 - 开始日期.我猜应该是一个应用类型函数.

Task 1. I need to calculate "Days", which is just the visitdate - the startdate, for each row. Should be an apply type function, I guess.

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

Task 2. I have to make a multiplot figure with one scatterplot for each of the count variables (a plot for count1, one for count2, etc). In each scatterplot, I need to plot the value of the count (y axis) against "Days" (x-axis) and connect the dots for each subject. Subjects in Treatgroup 0 are one color, subjects in treatgroup 1 are another color. Each scatterplot should be labeled with count1, count2 etc as appropriate.

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

I am trying to use the base plotting function, and have taken the approach of writing a plotting function to call later. I think this can work but need some help with syntax.

#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) 

推荐答案

任务 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")

这篇关于R中的多个散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆