0

我有以下数据框

 Op.1 Op.2 Site diet Horse ICS
 35   25    a    1     1  10
 32   31    a    1     2  10
 19   32    a    1     3  10
 17   26    a    1     4  10
 25   19    a    1     5  10
 25   17    a    1     6  10
 #... to 432 observations

我使用以下函数完成了 Bland-Altman 图:

 BAplot <- function(x,y,yAxisLim=c(-50,50),xlab="Average", ylab="Difference") {
   d <- ((x + y)/2)
   diff <- x - y        
   plot(diff ~ d,ylim=yAxisLim,xlim=c(0,60),xlab=xlab,ylab=ylab)
   abline(h=(mean(na.omit(diff))-c(-0.96,0,0.96)*sd(na.omit(diff))),lty=2)
 }

得到的情节很好。现在我正在尝试根据 data$Site(4 个级别:0,1,2,3)和形状根据 data$ICS 的级别(6 个级别:10,11,12,13,14,15)给出颜色

我写了以下代码:

 clr   <- c("a"="red","b"="blue","c"="green","d"="yellow")[data$Site]
 shape <- c("10"="0","11"="1","12"="2","13"="3","14"="4","15"="5")[data$ICS]
 plot.ops<-BAplot(data$Op.1,data$Op.2,xlab="(Op1 vs Op 2)/2", ylab="Op1-mean of aOp1+Op2",col=clr,pch=shape)

但它给出了错误

 Error in BAplot(data$Op.1, data$Op.2, xlab = "(Op1 vs Op 2)/2", ylab = "Op1-mean of Op1+Op2",  : 
    unused arguments (col = clr, pch = shape)

我还尝试更改形状 <- c(10=0,11=1,12=2...) 1,2,3 是 pch 中不同的形状类型,但它仍然不起作用。对 clr 也是如此。

我最终希望拥有的是具有不同颜色的“站点”和不同形状的“ICS”的情节。

这意味着非常简单,但我认为可能存在一个基本的概念错误,但我被卡住了。

我也会通过使用填充或清空的形状来添加饮食(2 个级别)......但在我首先得到这个排序之前无法进入那个阶段!

非常感谢,M

4

2 回答 2

0

我试图复制你的代码,问题是形状都是由NA.

这是因为它data$ICS是数字,而不是字符串。

您可以使用它来解决问题(注意我从数字中删除了引号,否则数字本身将用作形状,这非常难看:

shapes <- c("10"=0,"11"=1,"12"=2,"13"=3,"14"=4,"15"=5)[as.character(data$ICS)]

或者,更简单

shapes <- (1:5)[data$ICS-10]
于 2013-11-10T20:36:26.877 回答
0

这就是我最终成功的原因:

a<-ifelse(data$ICS==10,"a",ifelse(data$ICS==11,"b",ifelse(data$ICS==12,"c",ifelse(data$ICS==13,"d",ifelse(data$ICS==14,"e","f"))))) #ICS as characters
cls<-c(2,"orange",7,3,6,4) [factor(a)] #10-11-12-13-14-15: red,orange,yellow,green,purple,blue
b<-data$Site
shapes<-c(0,1,2,8)[factor(b)] #Square is RDC liv, Circle is RDC V, Triangle is RVC V, Star is RVC CCJ
BAplot <- function(x,y,yAxisLim=c(-50,50),xlab="Average", ylab="Difference",col=cls,pch=shapes) {
  d <- ((x + y)/2)
  diff <- x - y        
  plot(diff ~ d,ylim=yAxisLim,xlim=c(0,60),xlab=xlab,ylab=ylab,col=cls,pch=shapes)
  abline(h=(mean(na.omit(diff))-c(-0.96,0,0.96)*sd(na.omit(diff))),lty=2)
}
plot.ops<-BAplot(data$Op.1,data$Op.2,xlab="(Op1 vs Op 2)/2", ylab="Op1-mean of Op1+Op2",col=cls,pch=shapes)
title(main="Bland-Altman plots of Op1 vs Op2")
legend (34,53,legend=c("RDC Liver","RDC V","RVC V","RVC CCJ"), pch=c(0,1,2,8), pt.cex=2, y.intersp=0.8) #legend for shape
legend (49,53,legend=c("10th ICS","11th ICS","12th ICS","13th ICS","14th ICS","15th ICS"), pch=22, pt.cex=2, pt.bg=c(2,"orange",7,3,6,4), y.intersp=0.6) #legend for the colours

不知道为什么,但如果我写了它就行不通

shapes<-c(0,1,2,8)[factor(data$Site)]

它只有在我创建时才有效

b<-data$Site
shapes<-c(0,1,2,8)[factor(b)]

无论如何,现在整理!

非常感谢,马可

于 2013-11-11T16:22:35.177 回答