如果分面图上还想再添加文字,只能使用底层的grid包了。
函数定义
#' Add text to ggplot2 figures
#'
#' @param label text you want to put on figure
#' @param x position x, left is 0, right 1
#' @param y position y, bottom is 0, up 1
#' @param color text color
#' @param size font size
#'
#' @return
#' @export
#'
#' @examples
AddText=function(label="RNA cluster",x = 0.18,y = 0.035, color="red", size=12){library(grid)grid.text(label=label, x = x, y = y, gp=gpar(col=color, fontsize=size,draw=TRUE,just = "centre"))
}
例1:
在ggplot后执行,注意,不能是+号连接!
ggplot(mtcars, aes(mpg, wt)) + geom_point(); AddText("my note")
效果:
例2:
p1=ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme_classic()print(p1)print(AddText("my note"))print(AddText("another text", x=0.7, y=0.8, color = "navy"))