在多绘图循环中将子集绘制为数据标签

时间:2015-11-10 14:05:06

标签: r plot

我要做的是创建一个循环,根据合约规模和利润(差异)绘制我所有项目经理的项目。然后,我试图在各点之间绘制abline(),以确定他们未来项目的利润。最后,我想以特定标准(Contract >=500000 & Status =="A")标记所有点。

到目前为止,我已经弄清楚了大部分内容,但似乎无法弄清楚如何使用text()函数对数据进行子集化。相反,我最终得到的是一个包含所有标签而不是特定标签的情节。

这是我的数据框:

data <- data.frame(Project.Manager=c("Bert", "Ernie" ,"Elmo","Bert", "Ernie" ,"Elmo","Bert","Ernie","Elmo"),
               Contract=c(100000,15000,25000,588000,1250000,355000,50000,80000,335000),
               Variance=c(15000,-50000,2000,100000,550000,-180000,-15000,25000,100000),
               Project=c("Bert's House", "Ernie's House", "Elmo's House", "Oscar Trash Can Renovations", "Big Bird's Nest", "Sesame St. Renovations","Count's Corner","Tickle Me Elmo Factory", "Set Production"),
               Status=c("A","C","C","C","A","C","C","C","C"))

data$Color <- "black"
data$Color[data$Status =="A"]<-"green"

到目前为止,我的代码是3x1图。

par(mar=c(2,4,2,1))
par(mfrow=c(1,3))
par(cex=0.6)
xrange <- c(0,1500000)
ticks2 <- pretty(xrange)
labels2 <- format(ticks2, big.mark=",", scientific = FALSE)
yrange <- c(-200000,600000)
ticks <- pretty(yrange)
labels <- format(ticks, big.mark=",",scientific=FALSE)
pos_vector <- rep(4, length(data$Project))

by (data,data$Project.Manager,function(i){
  plot(i$Contract,i$Variance,main=unique(i$Project.Manager),yaxt="n",ylim=yrange,
  xaxt="n",xlim=xrange,xlab="Profit",ylab="",col=i$Color)
  with(data,text(i$Contract,i$Variance,labels=i$Project,cex=0.7,pos=pos_vector))
  axis(2, at =ticks,labels=labels,las=1,cex.axis=0.7)
  axis(1, at = ticks2,labels=labels2,las=1,cex.axis=0.7)
  abline(0,0,col="red")
  abline(lm(i$Variance~i$Contract,subset = i$Status =="C"),col="blue",lty=2)
})

现在,这是我的情节。理想情况下,我希望只有“Big Bird's House”才能显示为具有给定标准的数据标签。 enter image description here

0 个答案:

没有答案