0

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()
STL
  • 283
  • 1
  • 3
  • 13
  • 1
    Could you please make your [example reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – Roman Luštrik Sep 09 '19 at 18:49
  • Which line gives the error? We can't run any code to check since you haven't shared sample data... – Gregor Thomas Sep 09 '19 at 19:03
  • I've added some sample data to fit the code. The sample data dosen't shhow overlapped labels but in my actual data it does. I would like to edit the code so that there isn't any overlap. As for the error I get, it usually occurs under #Spider plot - textplot(). – STL Sep 09 '19 at 19:23
  • You could try ggplot with the ggrepel::geom_text_repel() function. – Bill O'Brien Sep 09 '19 at 19:47
  • Can I ask what parts of my code would need to modify? Originally I had chosen textplot() because it would leave the majority of my code the same. – STL Sep 09 '19 at 20:03

0 Answers0