plotfunction.R

trans_color = function(x){
  if (x == "g") return("red")
  else if (x == "c") return("orange")
  else if (x == "t") return("green")
  else return ("yellow")
}


DNA_plot <- function(data, ver_height = 5, seg=TRUE){
  if (!seg){
    col_ls <- sapply(data, trans_color)
    y = rep(ver_height, length(data))
    plot(1:length(data), y, pch=15, cex=5, col=col_ls, ylim = c(-10,10), xlab=" ", ylab=" ", xaxt="n", yaxt="n", main="DNA Plot")
    legend("top", legend=c("Adenine", "Thynine", "Cytosine", "Guanine")
           ,col = c("yellow", "green", "orange", "red"), pch=15, box.lty = 0, horiz=TRUE)
  }
  else {
    #par(bg="gray90")
    par(bg="white")
    obj = opt(data, measure=loglikhood, beta = 8)
    cp = c(1, obj$cp, length(data))
    col_ls <- sapply(data, trans_color)
    y = rep(ver_height, length(data))
    plot(1:length(data), y, pch=15, cex=6, col=col_ls, ylim = c(-length(obj$seg)*5-20,50), xlab=" ", ylab=" ", xaxt="n", yaxt="n", main="DNA Plot")

    for (i in 1: length(obj$seg)){
      col_ls = sapply(data[cp[i]:cp[i+1]], trans_color)
      points((cp[i]:cp[i + 1])-4, rep(i*-5, length(cp[i]:cp[i+1])), pch=15, cex=3, col=col_ls)
      #abline(v=cp[i], col="blue", lwd=3, lty=2)
    }
    legend("top", legend=c("Adenine", "Thynine", "Cytosine", "Guanine")
           ,col = c("yellow", "green", "orange", "red")
           ,pch=15, box.lty = 0, horiz=TRUE, bg = "white")
  }

}

DNA_plot(test, ver_height = 10,seg=TRUE)

abline(v=10)
aaaaana/changepoints documentation built on May 26, 2019, 1:35 p.m.