Annotating text on individual facet in ggplot2

前端 未结 6 926
野的像风
野的像风 2020-11-22 09:39

I want to annotate some text on last facet of the plot with the following code:

library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <-          


        
6条回答
  •  礼貌的吻别
    2020-11-22 10:11

    I think for the answer above lab="Text" is useless, the code below is also ok.

    ann_text <- data.frame(mpg = 15,wt = 5,
                           cyl = factor(8,levels = c("4","6","8")))
    p + geom_text(data = ann_text,label = "Text" )
    

    However if you want to label differently in different sub-graphs, it will be ok in this way:

    ann_text <- data.frame(mpg = c(14,15),wt = c(4,5),lab=c("text1","text2"),
                           cyl = factor(c(6,8),levels = c("4","6","8")))
    p + geom_text(data = ann_text,aes(label =lab) )
    

提交回复
热议问题