Add text to ggplot

后端 未结 1 432
猫巷女王i
猫巷女王i 2021-02-03 19:37

(updated) I have ggplot like this, but then the x axis Date scaled:

g1 <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()

Above two ba

相关标签:
1条回答
  • 2021-02-03 20:15

    Consider using annotate() to place whatever text where you want at a given location on the plot. Factor variables, as in the clarity factor on the x-axis, have a number for each level, so you can use that number to locate the text. I assume date variables have the same usage.:

    ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() +
      annotate("text", x=8, y=13000, label= "boat") + 
      annotate("text", x = 4, y=13000, label = "ship")
    

    enter image description here

    EDIT after COMMENT

    For efficiency, you can combine the annotations, such as this:

    ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() +
      annotate("text", x = c(2,4,6,8), y=13000, label = c("two", "ship", "six", "boat"))
    
    0 讨论(0)
提交回复
热议问题