From 2d5b50254383d561e0b5373bd442586193bc6296 Mon Sep 17 00:00:00 2001 From: LaoJunJun <64965509+junjunlab@users.noreply.github.com> Date: Mon, 18 Jul 2022 22:08:38 +0800 Subject: [PATCH 1/5] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index be28872..a7b8424 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,5 @@ An eaxmple shows here: More examples and details see: > **https://github.com/junjunlab/jjAnno/wiki/jjAnno-0.0.1** + +> **https://github.com/junjunlab/jjAnno/wiki/jjAnno-0.0.2** From 71a959a6894f6a9b35a72ec4cdc12c284dd37f50 Mon Sep 17 00:00:00 2001 From: LaoJunJun <64965509+junjunlab@users.noreply.github.com> Date: Fri, 22 Jul 2022 21:38:52 +0800 Subject: [PATCH 2/5] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index a7b8424..be28872 100644 --- a/README.md +++ b/README.md @@ -28,5 +28,3 @@ An eaxmple shows here: More examples and details see: > **https://github.com/junjunlab/jjAnno/wiki/jjAnno-0.0.1** - -> **https://github.com/junjunlab/jjAnno/wiki/jjAnno-0.0.2** From eb8e4caaef05cf853e7672c19a157d8ba190d8b5 Mon Sep 17 00:00:00 2001 From: LaoJunJun <64965509+junjunlab@users.noreply.github.com> Date: Fri, 22 Jul 2022 23:04:20 +0800 Subject: [PATCH 3/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index be28872..f20210d 100644 --- a/README.md +++ b/README.md @@ -27,4 +27,4 @@ An eaxmple shows here: More examples and details see: -> **https://github.com/junjunlab/jjAnno/wiki/jjAnno-0.0.1** +> **https://junjunlab.github.io/jjAnno-manual/** From 56f2c8be255baa21aa3554ec26e2ad08a20d4d08 Mon Sep 17 00:00:00 2001 From: zhangjun-123 <64965509+junjunlab@users.noreply.github.com> Date: Sat, 23 Jul 2022 15:44:51 +0800 Subject: [PATCH 4/5] big improvements --- DESCRIPTION | 3 +- NAMESPACE | 1 + R/annoRect.R | 445 +++++++++++++++++++++++++++++++++++++------- R/annoTriangle.R | 380 +++++++++++++++++++++++++++++++++++++ man/annoRect.Rd | 14 ++ man/annoTriangle.Rd | 87 +++++++++ 6 files changed, 866 insertions(+), 64 deletions(-) create mode 100644 R/annoTriangle.R create mode 100644 man/annoTriangle.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 5ffe616..9071fd8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: jjAnno -Title: An annotation package for ggplot plot +Title: An annotation package for ggplot output Version: 0.0.2 Authors@R: person("Junjun", "Lao", , "3219030654@stu.cpu.edu.cn", role = c("aut", "cre"), @@ -13,6 +13,7 @@ Imports: dplyr, ggiraphExtra, ggplot2, + grDevices, grid, magick, magrittr diff --git a/NAMESPACE b/NAMESPACE index bfb3197..c743e88 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,5 +7,6 @@ export(annoPoint) export(annoPoint2) export(annoRect) export(annoSegment) +export(annoTriangle) export(useMyCol) importFrom(magrittr,"%>%") diff --git a/R/annoRect.R b/R/annoRect.R index 7058021..6a15399 100644 --- a/R/annoRect.R +++ b/R/annoRect.R @@ -15,6 +15,8 @@ #' @param lty The rect line type. Default(NULL). #' @param lwd The rect line width. Default(NULL). #' @param alpha The rect fill color alpha. Default(NULL). +#' @param roundRect Whether add roundRect instead of rect. Default(FALSE). +#' @param roundRadius The roundRect corner radius. Default(0.1). #' @param annoManual Whether annotate by yourself by supplying with x and y coordinates. Default(FALSE). #' @param addText Whether add text label on segment. Default(FALSE). #' @param textCol The text colors. Default(NULL). @@ -27,6 +29,12 @@ #' @param hjust The text hjust. Default(NULL). #' @param vjust The text vjust. Default(NULL). #' +#' @param textShift The text label shift size. Default(0). +#' @param rotateRect Whether to rotate the rect annotation. Default(FALSE). +#' @param normRectShift The "top" or "right" rotated rect shift. Default(0). +#' @param rotatedRectShift The "botomn" or "left" rotated rect shift. Default(1). +#' @param rectAngle Whether rotate the rect with specified degree. Default(NULL). +#' #' @return Return a ggplot object. #' @export #' @@ -80,6 +88,8 @@ annoRect <- function(object = NULL, lty = NULL, lwd = NULL, alpha = NULL, + roundRect = FALSE, + roundRadius = 0.1, annoManual = FALSE, addText = FALSE, textCol = NULL, @@ -89,8 +99,13 @@ annoRect <- function(object = NULL, textLabel = NULL, textRot = 0, textHVjust = 0.2, + textShift = 0, hjust = NULL, - vjust = NULL){ + vjust = NULL, + rotateRect = FALSE, + normRectShift = 0, + rotatedRectShift = 1, + rectAngle = NULL){ # ============================================================================ # get data data <- object$data @@ -225,6 +240,30 @@ annoRect <- function(object = NULL, nPoints <- max(length(xmin),length(ymin)) } + + # ============================================================================ + # whether supply a rectAngle + if(!is.null(rectAngle)){ + text_angle <- rectAngle + }else{ + if(annoPos %in% c('top','botomn')){ + # retrive text angle + text_angle <- object$theme$axis.text.x$angle + }else if(annoPos %in% c('left','right')){ + # retrive text angle + text_angle <- object$theme$axis.text.y$angle + } + + # test special degree + if(!is.null(text_angle)){ + if(text_angle %in% c(0,90,180,270,360)){ + text_angle <- 0 + }else{ + text_angle <- text_angle + } + }else{} + } + # ============================================================================ # color if(is.null(pCol) & is.null(pFill)){ @@ -242,68 +281,344 @@ annoRect <- function(object = NULL, } # ============================================================================ - if(annoPos %in% c('top','botomn')){ - if(!is.list(yPosition)){ - # plot - for (i in 1:nPoints) { - object <- object + - # add points - ggplot2::annotation_custom( - grob = grid::rectGrob(gp = grid::gpar(col = pCol[i], - fill = pFill[i], - lty = lty, - lwd = lwd, - alpha = alpha)), - xmin = ggplot2::unit(xmin[i],'native'),xmax = ggplot2::unit(xmax[i],'native'), - ymin = ggplot2::unit(ymin,'native'),ymax = ggplot2::unit(ymax,'native')) + if(roundRect == FALSE){ + if(annoPos %in% c('top','botomn')){ + if(!is.list(yPosition)){ + #################################### + # whether totate rect + if(rotateRect == FALSE){ + # plot + for (i in 1:nPoints) { + object <- object + + # add points + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = pCol[i], + fill = pFill[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin,'native'), + ymax = ggplot2::unit(ymax,'native')) + } + }else{ + #################################### + # rotate rect + if(annoPos == 'top'){ + for (i in 1:nPoints) { + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(x = c(0, + tan(pi*((90 - text_angle)/180))/abs(xmax[i] - xmin[i]), + 1 + tan(pi*((90 - text_angle)/180))/abs(xmax[i] - xmin[i]), + 1), + y = c(0,1,1,0), + gp = grid::gpar(fill = pFill[i], + col = pCol[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin[i] - normRectShift,'native'), + xmax = ggplot2::unit(xmax[i] - normRectShift,'native'), + ymin = ggplot2::unit(ymin,'native'), + ymax = ggplot2::unit(ymax,'native')) + + } + }else{ + for (i in 1:nPoints) { + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(x = c(0, + tan(pi*((90 - text_angle)/180))/abs(xmax[i] - xmin[i]), + 1 + tan(pi*((90 - text_angle)/180))/abs(xmax[i] - xmin[i]), + 1), + y = c(0,1,1,0), + gp = grid::gpar(fill = pFill[i], + col = pCol[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin[i] - rotatedRectShift,'native'), + xmax = ggplot2::unit(xmax[i] - rotatedRectShift,'native'), + ymin = ggplot2::unit(ymin,'native'), + ymax = ggplot2::unit(ymax,'native')) + + } + } + } + }else{ + #################################### + # whether totate rect + if(rotateRect == FALSE){ + # plot + for (i in 1:nPoints) { + object <- object + + # add points + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = pCol[i], + fill = pFill[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native')) + } + }else{ + #################################### + # rotate rect + if(annoPos == 'top'){ + for (i in 1:nPoints) { + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(x = c(0, + tan(pi*((90 - text_angle)/180))/abs(xmax[i] - xmin[i]), + 1 + tan(pi*((90 - text_angle)/180))/abs(xmax[i] - xmin[i]), + 1), + y = c(0,1,1,0), + gp = grid::gpar(fill = pFill[i], + col = pCol[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin[i] - normRectShift,'native'), + xmax = ggplot2::unit(xmax[i] - normRectShift,'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native')) + + } + }else{ + for (i in 1:nPoints) { + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(x = c(0, + tan(pi*((90 - text_angle)/180))/abs(xmax[i] - xmin[i]), + 1 + tan(pi*((90 - text_angle)/180))/abs(xmax[i] - xmin[i]), + 1), + y = c(0,1,1,0), + gp = grid::gpar(fill = pFill[i], + col = pCol[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin[i] - rotatedRectShift,'native'), + xmax = ggplot2::unit(xmax[i] - rotatedRectShift,'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native')) + + } + } + } } - }else{ - # plot - for (i in 1:nPoints) { - object <- object + - # add points - ggplot2::annotation_custom( - grob = grid::rectGrob(gp = grid::gpar(col = pCol[i], - fill = pFill[i], - lty = lty, - lwd = lwd, - alpha = alpha)), - xmin = ggplot2::unit(xmin[i],'native'),xmax = ggplot2::unit(xmax[i],'native'), - ymin = ggplot2::unit(ymin[i],'native'),ymax = ggplot2::unit(ymax[i],'native')) + }else if(annoPos %in% c('left','right')){ + if(!is.list(xPosition)){ + #################################### + # whether totate rect + if(rotateRect == FALSE){ + # plot + for (i in 1:nPoints) { + object <- object + + # add points + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = pCol[i], + fill = pFill[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin,'native'), + xmax = ggplot2::unit(xmax,'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native')) + } + }else{ + #################################### + # rotate rect + if(annoPos == 'left'){ + for (i in 1:nPoints) { + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(y = c(0, + tan(pi*((90 - text_angle)/180))/abs(ymax[i] - ymin[i]), + 1 + tan(pi*((90 - text_angle)/180))/abs(ymax[i] - ymin[i]), + 1), + x = c(0,1,1,0), + gp = grid::gpar(fill = pFill[i], + col = pCol[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin,'native'), + xmax = ggplot2::unit(xmax,'native'), + ymin = ggplot2::unit(ymin[i] - rotatedRectShift,'native'), + ymax = ggplot2::unit(ymax[i] - rotatedRectShift,'native')) + + } + }else{ + for (i in 1:nPoints) { + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(y = c(0, + tan(pi*((90 - text_angle)/180))/abs(ymax[i] - ymin[i]), + 1 + tan(pi*((90 - text_angle)/180))/abs(ymax[i] - ymin[i]), + 1), + x = c(0,1,1,0), + gp = grid::gpar(fill = pFill[i], + col = pCol[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin,'native'), + xmax = ggplot2::unit(xmax,'native'), + ymin = ggplot2::unit(ymin[i] - normRectShift,'native'), + ymax = ggplot2::unit(ymax[i] - normRectShift,'native')) + + } + } + } + }else{ + #################################### + # whether totate rect + if(rotateRect == FALSE){ + # plot + for (i in 1:nPoints) { + object <- object + + # add points + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = pCol[i], + fill = pFill[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native')) + } + }else{ + #################################### + # rotate rect + if(annoPos == 'left'){ + for (i in 1:nPoints) { + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(y = c(0, + tan(pi*((90 - text_angle)/180))/abs(ymax[i] - ymin[i]), + 1 + tan(pi*((90 - text_angle)/180))/abs(ymax[i] - ymin[i]), + 1), + x = c(0,1,1,0), + gp = grid::gpar(fill = pFill[i], + col = pCol[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin[i] - normRectShift,'native'), + ymax = ggplot2::unit(ymax[i] - normRectShift,'native')) + + } + }else{ + for (i in 1:nPoints) { + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(y = c(0, + tan(pi*((90 - text_angle)/180))/abs(ymax[i] - ymin[i]), + 1 + tan(pi*((90 - text_angle)/180))/abs(ymax[i] - ymin[i]), + 1), + x = c(0,1,1,0), + gp = grid::gpar(fill = pFill[i], + col = pCol[i], + lty = lty, + lwd = lwd, + alpha = alpha)), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin[i] - rotatedRectShift,'native'), + ymax = ggplot2::unit(ymax[i] - rotatedRectShift,'native')) + + } + } + } } - } - }else if(annoPos %in% c('left','right')){ - if(!is.list(xPosition)){ - # plot - for (i in 1:nPoints) { - object <- object + - # add points - ggplot2::annotation_custom( - grob = grid::rectGrob(gp = grid::gpar(col = pCol[i], - fill = pFill[i], - lty = lty, - lwd = lwd, - alpha = alpha)), - xmin = ggplot2::unit(xmin,'native'),xmax = ggplot2::unit(xmax,'native'), - ymin = ggplot2::unit(ymin[i],'native'),ymax = ggplot2::unit(ymax[i],'native')) + }else{} + }else{ + # round rect corner + if(annoPos %in% c('top','botomn')){ + if(!is.list(yPosition)){ + # plot + for (i in 1:nPoints) { + object <- object + + # add points + ggplot2::annotation_custom( + grob = grid::roundrectGrob(gp = grid::gpar(col = pCol[i], + fill = pFill[i], + lty = lty, + lwd = lwd, + alpha = alpha), + r = ggplot2::unit(roundRadius, "snpc")), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin,'native'), + ymax = ggplot2::unit(ymax,'native')) + } + }else{ + # plot + for (i in 1:nPoints) { + object <- object + + # add points + ggplot2::annotation_custom( + grob = grid::roundrectGrob(gp = grid::gpar(col = pCol[i], + fill = pFill[i], + lty = lty, + lwd = lwd, + alpha = alpha), + r = ggplot2::unit(roundRadius, "snpc")), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native')) + } } - }else{ - # plot - for (i in 1:nPoints) { - object <- object + - # add points - ggplot2::annotation_custom( - grob = grid::rectGrob(gp = grid::gpar(col = pCol[i], - fill = pFill[i], - lty = lty, - lwd = lwd, - alpha = alpha)), - xmin = ggplot2::unit(xmin[i],'native'),xmax = ggplot2::unit(xmax[i],'native'), - ymin = ggplot2::unit(ymin[i],'native'),ymax = ggplot2::unit(ymax[i],'native')) + }else if(annoPos %in% c('left','right')){ + if(!is.list(xPosition)){ + # plot + for (i in 1:nPoints) { + object <- object + + # add points + ggplot2::annotation_custom( + grob = grid::roundrectGrob(gp = grid::gpar(col = pCol[i], + fill = pFill[i], + lty = lty, + lwd = lwd, + alpha = alpha), + r = ggplot2::unit(roundRadius, "snpc")), + xmin = ggplot2::unit(xmin,'native'), + xmax = ggplot2::unit(xmax,'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native')) + } + }else{ + # plot + for (i in 1:nPoints) { + object <- object + + # add points + ggplot2::annotation_custom( + grob = grid::roundrectGrob(gp = grid::gpar(col = pCol[i], + fill = pFill[i], + lty = lty, + lwd = lwd, + alpha = alpha), + r = ggplot2::unit(roundRadius, "snpc")), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native')) + } } - } - - }else{} + }else{} + } # ============================================================================ # text color @@ -338,8 +653,10 @@ annoRect <- function(object = NULL, check.overlap = T, just = "centre", rot = textRot), - xmin = ggplot2::unit(xmin[i],'native'),xmax = ggplot2::unit(xmax[i],'native'), - ymin = ggplot2::unit(ymin + textHVjust,'native'),ymax = ggplot2::unit(ymax + textHVjust,'native')) + xmin = ggplot2::unit(xmin[i] + textShift,'native'), + xmax = ggplot2::unit(xmax[i] + textShift,'native'), + ymin = ggplot2::unit(ymin + textHVjust,'native'), + ymax = ggplot2::unit(ymax + textHVjust,'native')) } }else if(addText == TRUE & annoPos %in% c('left','right')){ # plot @@ -357,8 +674,10 @@ annoRect <- function(object = NULL, check.overlap = T, just = "centre", rot = textRot), - xmin = ggplot2::unit(xmin + textHVjust,'native'),xmax = ggplot2::unit(xmax + textHVjust,'native'), - ymin = ggplot2::unit(ymin[i],'native'),ymax = ggplot2::unit(ymax[i],'native')) + xmin = ggplot2::unit(xmin + textHVjust,'native'), + xmax = ggplot2::unit(xmax + textHVjust,'native'), + ymin = ggplot2::unit(ymin[i] + textShift,'native'), + ymax = ggplot2::unit(ymax[i] + textShift,'native')) } }else{} diff --git a/R/annoTriangle.R b/R/annoTriangle.R new file mode 100644 index 0000000..59304c1 --- /dev/null +++ b/R/annoTriangle.R @@ -0,0 +1,380 @@ +#' @title annoTriangle +#' @name annoTriangle +#' @author Junjun Lao +#' @description This function is used to add triangle annotations in plot. +#' @param object Your ggplot list. Default(NULL). +#' @param relSideDist The relative distance ratio to the y axis range. Default(0.1). +#' @param annoPos The position for the annotation to be added. Default("top"). +#' @param xPosition The x axis coordinate for the triangle. Default(NULL). +#' @param yPosition The y axis coordinate for the triangle. Default(NULL). +#' @param fillCol The triangle fill colors. Default(NULL). +#' @param nCol The colors bins. Default(100). +#' @param addTriangle Whether add triangle annotation. Default("TRUE"). +#' @param triangleType The triangle shape type, "RU"(right-up), "RD"(right-down), "LU"(left-up), "LD"(left-down). Default("RD"). +#' @param addBorder Whether add border to triangle or rect annotation. Default("FALSE"). +#' @param borderCol The border color. Default("black"). +#' @param lty The border lty. Default(NULL). +#' @param lwd The border lwd. Default(NULL). +#' +#' @return Return a ggplot object. +#' @export +#' +#' @examples +#'# =============================== +#'# test function +#' +#'# load test data +#'data(p) +#' +#'p1 <- p + +#' ggplot2::theme(plot.margin = ggplot2::margin(t = 2,unit = 'cm')) +#' +#'# default plot +#'annoTriangle(object = p1, +#' annoPos = 'top', +#' xPosition = c(0,10.5)) +#' +#'# ajust yposition +#'annoTriangle(object = p1, +#' annoPos = 'top', +#' xPosition = c(0.5,10.5), +#' yPosition = c(10.8,11.5)) +#' +#'# add border +#'annoTriangle(object = p1, +#' annoPos = 'top', +#' xPosition = c(0.5,10.5), +#' yPosition = c(10.8,11.5), +#' addBorder = TRUE, +#' lwd = 2.5) + + +# define function +annoTriangle <- function(object = NULL, + relSideDist = 0.1, + annoPos = 'top', + xPosition = NULL, + yPosition = NULL, + fillCol = NULL, + nCol = 100, + addTriangle = TRUE, + triangleType = "RD", + addBorder = FALSE, + borderCol = 'black', + lty = NULL, + lwd = NULL){ + # ============================================================================ + # get data + data <- object$data + + # get mapping variables + aes_x <- ggiraphExtra::getMapping(object$mapping,"x") + aes_y <- ggiraphExtra::getMapping(object$mapping,"y") + + # test variable type + data_x <- data[,c(aes_x)] + data_y <- data[,c(aes_y)] + + # colors + if(is.null(fillCol)){ + col <- grDevices::colorRampPalette(useMyCol('greenBlue',7))(nCol) + }else{ + col <- grDevices::colorRampPalette(fillCol)(nCol) + } + + # ============================================================================ + # calculate legend relative pos + # annotation position + if(annoPos %in% c('top','botomn')){ + + # xPos + xPos <- xPosition + + # calculate bin + ratioSeg <- (xPos[2] - xPos[1] + 1)/nCol + + # xmin and xmax + xmin <- seq(xPos[1],xPos[2],ratioSeg) + xmax <- seq(xPos[1],xPos[2] - ratioSeg,ratioSeg) + ratioSeg + + # length + nPoints <- length(xmin) + + # not supply yPos auto calculate + if(is.null(yPosition)){ + # numeric or discrete + if(is.numeric(data_y)){ + if(annoPos == 'top'){ + ymax <- max(data_y) + relSideDist*max(data_y) + ymin <- max(data_y) + }else{ + ymin <- min(data_y) - relSideDist*max(data_y) + ymax <- min(data_y) + } + }else{ + if(annoPos == 'top'){ + ymax <- length(unique(data_y)) + relSideDist*length(unique(data_y)) + ymin <- length(unique(data_y)) + }else{ + ymax <- 0 + ymin <- -relSideDist*length(unique(data_y)) + } + } + }else{ + ymax <- yPosition[1] + ymin <- yPosition[2] + } + + }else if(annoPos %in% c('left','right')){ + + # yPos + yPos <- yPosition + + # calculate bin + ratioSeg <- (yPos[2] - yPos[1] + 1)/nCol + + # xmin and xmax + ymin <- seq(yPos[1],yPos[2],ratioSeg) + ymax <- seq(yPos[1],yPos[2] - ratioSeg,ratioSeg) + ratioSeg + + # length + nPoints <- length(ymin) + + # not supply xPos auto calculate + if(is.null(xPosition)){ + # numeric or discrete + if(is.numeric(data_x)){ + if(annoPos == 'left'){ + xmin <- min(data_x) - relSideDist*max(data_x) + xmax <- min(data_x) + }else{ + xmax <- max(data_x) + relSideDist*max(data_x) + xmin <- max(data_x) + } + }else{ + if(annoPos == 'left'){ + xmin <- -relSideDist*length(unique(data_x)) + xmax <- 0 + }else{ + xmax <- length(unique(data_x)) + relSideDist*length(unique(data_x)) + xmin <- length(unique(data_x)) + } + } + }else{ + xmin <- xPosition[1] + xmax <- xPosition[2] + } + } + + # ============================================================================ + # triangle type + type_rd <- list(x = c(0,0,1),y = c(0,1,1)) + type_lu <- list(x = c(0,1,1),y = c(0,0,1)) + type_ru <- list(x = c(0,0,1),y = c(0,1,0)) + type_ld <- list(x = c(0,1,1),y = c(1,0,1)) + + # choose triangle type to show + if(triangleType == "RD"){ + triType <- type_rd + }else if(triangleType == "LU"){ + triType <- type_lu + }else if(triangleType == "RU"){ + triType <- type_ru + }else if(triangleType == "LD"){ + triType <- type_ld + } + + # border position + if(triangleType == "RD"){ + borderType <- type_lu + }else if(triangleType == "LU"){ + borderType <- type_rd + }else if(triangleType == "RU"){ + borderType <- type_ld + }else if(triangleType == "LD"){ + borderType <- type_ru + } + + # ============================================================================ + if(annoPos %in% c('top','botomn')){ + if(addTriangle == TRUE){ + for (i in 1:nPoints){ + ################################################## + # 1.add rect + object <- object + + # add triangle + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = "transparent", + fill = col[i], + lty = NULL, + lwd = NULL)), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin,'native'), + ymax = ggplot2::unit(ymax,'native') + ) + } + ################################################## + # 2. add triangle + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(x = triType[[1]], + y = triType[[2]], + gp = grid::gpar(fill = 'white', + col = 'white')), + xmin = ggplot2::unit(min(xmin),'native'), + xmax = ggplot2::unit(max(xmax),'native'), + ymin = ggplot2::unit(ymin,'native'), + ymax = ggplot2::unit(ymax,'native') + ) + + ################################################## + # 3. add border + if(addBorder == TRUE){ + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(x = borderType[[1]], + y = borderType[[2]], + gp = grid::gpar(fill = 'transparent', + col = borderCol, + lty = lty, + lwd = lwd)), + xmin = ggplot2::unit(min(xmin),'native'), + xmax = ggplot2::unit(max(xmax),'native'), + ymin = ggplot2::unit(ymin,'native'), + ymax = ggplot2::unit(ymax,'native') + ) + }else{ + object <- object + } + }else{ + for (i in 1:nPoints){ + ################################################## + # 1.add rect + object <- object + + # add triangle + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = "transparent", + fill = col[i], + lty = NULL, + lwd = NULL)), + xmin = ggplot2::unit(xmin[i],'native'), + xmax = ggplot2::unit(xmax[i],'native'), + ymin = ggplot2::unit(ymin,'native'), + ymax = ggplot2::unit(ymax,'native') + ) + } + + ################################################## + # 3. add border + if(addBorder == TRUE){ + object <- object + + # add triangle + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = borderCol, + fill = "transparent", + lty = lty, + lwd = lwd)), + xmin = ggplot2::unit(min(xmin),'native'), + xmax = ggplot2::unit(max(xmax),'native'), + ymin = ggplot2::unit(ymin,'native'), + ymax = ggplot2::unit(ymax,'native') + ) + }else{ + object <- object + } + } + }else if(annoPos %in% c('left','right')){ + if(addTriangle == TRUE){ + for (i in 1:nPoints){ + ################################################## + # 1.add rect + object <- object + + # add triangle + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = "transparent", + fill = col[i], + lty = NULL, + lwd = NULL)), + xmin = ggplot2::unit(xmin,'native'), + xmax = ggplot2::unit(xmax,'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native') + ) + } + ################################################## + # 2. add triangle + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(x = triType[[1]], + y = triType[[2]], + gp = grid::gpar(fill = 'white', + col = 'white')), + xmin = ggplot2::unit(xmin,'native'), + xmax = ggplot2::unit(xmax,'native'), + ymin = ggplot2::unit(min(ymin),'native'), + ymax = ggplot2::unit(max(ymax),'native') + ) + + ################################################## + # 3. add border + if(addBorder == TRUE){ + object <- object + + ggplot2::annotation_custom( + grob = grid::polygonGrob(x = borderType[[1]], + y = borderType[[2]], + gp = grid::gpar(fill = 'transparent', + col = borderCol, + lty = lty, + lwd = lwd)), + xmin = ggplot2::unit(xmin,'native'), + xmax = ggplot2::unit(xmax,'native'), + ymin = ggplot2::unit(min(ymin),'native'), + ymax = ggplot2::unit(max(ymax),'native') + ) + }else{ + object <- object + } + }else{ + for (i in 1:nPoints){ + ################################################## + # 1.add rect + object <- object + + # add triangle + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = "transparent", + fill = col[i], + lty = NULL, + lwd = NULL)), + xmin = ggplot2::unit(xmin,'native'), + xmax = ggplot2::unit(xmax,'native'), + ymin = ggplot2::unit(ymin[i],'native'), + ymax = ggplot2::unit(ymax[i],'native') + ) + } + + ################################################## + # 3. add border + if(addBorder == TRUE){ + object <- object + + # add triangle + ggplot2::annotation_custom( + grob = grid::rectGrob(gp = grid::gpar(col = borderCol, + fill = "transparent", + lty = lty, + lwd = lwd)), + xmin = ggplot2::unit(xmin,'native'), + xmax = ggplot2::unit(xmax,'native'), + ymin = ggplot2::unit(min(ymin),'native'), + ymax = ggplot2::unit(max(ymax),'native') + ) + }else{ + object <- object + } + } + }else{} + + # ============================================================================ + # print + print(object) +} diff --git a/man/annoRect.Rd b/man/annoRect.Rd index 7b6d2bd..abb6fce 100644 --- a/man/annoRect.Rd +++ b/man/annoRect.Rd @@ -30,6 +30,10 @@ \item{alpha}{The rect fill color alpha. Default(NULL).} +\item{roundRect}{Whether add roundRect instead of rect. Default(FALSE).} + +\item{roundRadius}{The roundRect corner radius. Default(0.1).} + \item{annoManual}{Whether annotate by yourself by supplying with x and y coordinates. Default(FALSE).} \item{addText}{Whether add text label on segment. Default(FALSE).} @@ -51,6 +55,16 @@ \item{hjust}{The text hjust. Default(NULL).} \item{vjust}{The text vjust. Default(NULL).} + +\item{textShift}{The text label shift size. Default(0).} + +\item{rotateRect}{Whether to rotate the rect annotation. Default(FALSE).} + +\item{normRectShift}{The "top" or "right" rotated rect shift. Default(0).} + +\item{rotatedRectShift}{The "botomn" or "left" rotated rect shift. Default(1).} + +\item{rectAngle}{Whether rotate the rect with specified degree. Default(NULL).} } \value{ Return a ggplot object. diff --git a/man/annoTriangle.Rd b/man/annoTriangle.Rd new file mode 100644 index 0000000..8a6e0ad --- /dev/null +++ b/man/annoTriangle.Rd @@ -0,0 +1,87 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/annoTriangle.R +\name{annoTriangle} +\alias{annoTriangle} +\title{annoTriangle} +\usage{ +annoTriangle( + object = NULL, + relSideDist = 0.1, + annoPos = "top", + xPosition = NULL, + yPosition = NULL, + fillCol = NULL, + nCol = 100, + addTriangle = TRUE, + triangleType = "RD", + addBorder = FALSE, + borderCol = "black", + lty = NULL, + lwd = NULL +) +} +\arguments{ +\item{object}{Your ggplot list. Default(NULL).} + +\item{relSideDist}{The relative distance ratio to the y axis range. Default(0.1).} + +\item{annoPos}{The position for the annotation to be added. Default("top").} + +\item{xPosition}{The x axis coordinate for the triangle. Default(NULL).} + +\item{yPosition}{The y axis coordinate for the triangle. Default(NULL).} + +\item{fillCol}{The triangle fill colors. Default(NULL).} + +\item{nCol}{The colors bins. Default(100).} + +\item{addTriangle}{Whether add triangle annotation. Default("TRUE").} + +\item{triangleType}{The triangle shape type, "RU"(right-up), "RD"(right-down), "LU"(left-up), "LD"(left-down). Default("RD").} + +\item{addBorder}{Whether add border to triangle or rect annotation. Default("FALSE").} + +\item{borderCol}{The border color. Default("black").} + +\item{lty}{The border lty. Default(NULL).} + +\item{lwd}{The border lwd. Default(NULL).} +} +\value{ +Return a ggplot object. +} +\description{ +This function is used to add triangle annotations in plot. +} +\examples{ +# =============================== +# test function + +# load test data +data(p) + +p1 <- p + + ggplot2::theme(plot.margin = ggplot2::margin(t = 2,unit = 'cm')) + +# default plot +annoTriangle(object = p1, + annoPos = 'top', + xPosition = c(0,10.5)) + +# ajust yposition +annoTriangle(object = p1, + annoPos = 'top', + xPosition = c(0.5,10.5), + yPosition = c(10.8,11.5)) + +# add border +annoTriangle(object = p1, + annoPos = 'top', + xPosition = c(0.5,10.5), + yPosition = c(10.8,11.5), + addBorder = TRUE, + lwd = 2.5) +} +\author{ +Junjun Lao +} From e31563238db18cc19f7151d521b920ea7323c482 Mon Sep 17 00:00:00 2001 From: zhangjun-123 <64965509+junjunlab@users.noreply.github.com> Date: Sun, 24 Jul 2022 15:09:59 +0800 Subject: [PATCH 5/5] Add hgap and vgap for annoLegend --- R/annoLegend.R | 7 +++++++ man/annoLegend.Rd | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/R/annoLegend.R b/R/annoLegend.R index f0379ad..fd94a85 100644 --- a/R/annoLegend.R +++ b/R/annoLegend.R @@ -17,6 +17,9 @@ #' @param fontfamily Legend text fontfamily. Default(NULL). #' @param fontface Legend text fontface. Default(NULL). #' +#' @param vgap Vertical space between the legend entries. Default(1). +#' @param hgap Horizontal space between the legend entries. Default(1). +#' #' @return Return a ggplot object. #' @export #' @@ -50,6 +53,8 @@ annoLegend <- function(object = NULL, xPosition = NULL, yPosition = NULL, labels = NULL, + vgap = 1, + hgap = 1, pch = NULL, ncol = 1, col = NULL, @@ -96,6 +101,8 @@ annoLegend <- function(object = NULL, ncol = ncol, do.lines = do.lines, lines.first = lines.first, + vgap = vgap, + hgap = hgap, gp = grid::gpar(col = col, fill = fill, fontsize = textSize, diff --git a/man/annoLegend.Rd b/man/annoLegend.Rd index 1f3aa18..c318423 100644 --- a/man/annoLegend.Rd +++ b/man/annoLegend.Rd @@ -10,6 +10,8 @@ annoLegend( xPosition = NULL, yPosition = NULL, labels = NULL, + vgap = 1, + hgap = 1, pch = NULL, ncol = 1, col = NULL, @@ -32,6 +34,10 @@ annoLegend( \item{labels}{The legend text labels. Default(NULL).} +\item{vgap}{Vertical space between the legend entries. Default(1).} + +\item{hgap}{Horizontal space between the legend entries. Default(1).} + \item{pch}{Legend shape. Default(NULL).} \item{ncol}{Legend columns to show. Default(NULL).}