I have the following function used to create a plot. The text overlaps and I tried using textplot instead of text to remove the overlap http://blog.fellstat.com/?cat=1 but I receive the error "Error in if (sdx == 0) sdx <- 1 : missing value where TRUE/FALSE needed". Any suggestions on how can I get the text to not overlap.
dat <- data.frame(GROUP = c("groupA", "groupA", "groupA", "group B"),
cohort = c("group 1", "group 1", "group 2", "group 2"),
ADY.m = c(21.054, 22.04, -0.17454, 0.0517454),
ID = c("A", "A", "B", "B"),
PCHG = c(-0.1, 0.1, 0.3, 0.2),
symbolC = c(1, 1, 1, 1),
ANNOTATION = c("A", "A", "B", "B"),
adym_last = c(21.0754, 22.034, -0.1354, 0.077454),
pchg_last = c(2, 2, 1, 1))
chrts <- unique(dat$cohort)
xmin <- floor(min(dat$ADY.m))
xmax <- ceiling(max(dat$ADY.m))
for (i in 1:length(chrts)){
datC <- subset(a, cohort == chrts[i])
datC <- datC[order(datC$ID,-datC$ADY.m),]
id <- unique(datC$ID)
nc <- length(id)
par(mgp=c(2,1,0))
plot (x=c(xmin, xmax), y=c(ymin,ymax),
type="n",
main=paste("label", datC$cohort[1]),
xlab="Time", xaxt="n",
ylab="", ylim=c(ymin,ymax), yaxt="n"
)
title(ylab="label")
# Axis
axis(1, at=seq(xmin, xmax,by=1), labels=seq(xmin, xmax,by=1), cex.axis=0.8)
axis(2, at=seq(ymin,ymax, by=10), labels=seq(ymin,ymax, by=10))
# Reference Line
abline(a=0, b=0, lty=1)
# HGG
abline(a=25, b=0, lty=3)
abline(a=-50, b=0, lty=3)
# Other
abline(a=20, b=0, lty=2)
abline(a=-30, b=0, lty=2)
# Spider Plot
for (j in 1:nc){
datP <- subset(datC, ID == id[j])
lines(datP$ADY.m,datP$PCHG, type="l", col=as.character(datP$colorC[1]), pch=datP$symbolC[1], lwd=2)
points(datP$ADY.m,datP$PCHG, pch=19, col=as.character(datP$colorC[1]))
text(datP$adym_last[1], datP$pchg_last[1], datP$ANNOTATION[1], cex=1)
# textplot(datP$ADY.m[1], min(datP$PCHG[1],100),id[j], pos = 1)
}
# Legend
legend("topright",
legend=c(GROUP[1:length(GROUP)], "label", "label", "label"),
col = c(colorC[1:length(GROUP)],"black", "black", "black"),
lty = c(rep(1, length(GROUP)),2,3, NA),
lwd = c(rep(1.5, length(GROUP)), 1.5, 1.5, 1),
pch = c(rep(NA,length(GROUP)),NA,NA, 8),
cex = 0.8,
bty ="n")
}
}
dev.off()