ggplot2 - annotate outside of plot

前端 未结 3 1708
青春惊慌失措
青春惊慌失措 2020-11-22 06:32

I would like to associate sample size values with points on a plot. I can use geom_text to position the numbers near the points, but this is messy. It would b

3条回答
  •  粉色の甜心
    2020-11-22 07:05

    Simplier solution based on grid

    require(grid)
    
    df = data.frame(y = c("cat1", "cat2", "cat3"), x = c(12, 10, 14), n = c(5, 15, 20))
    
    p <- ggplot(df, aes(x, y)) + geom_point() + # Base plot
    theme(plot.margin = unit(c(1, 3, 1, 1), "lines"))
    
    p
    
    grid.text("20", x = unit(0.91, "npc"), y = unit(0.80, "npc"))
    grid.text("15", x = unit(0.91, "npc"), y = unit(0.56, "npc"))
    grid.text("5", x = unit(0.91, "npc"), y = unit(0.31, "npc"))
    

提交回复
热议问题