Can't label multi-panel figure in r while using ggplot

后端 未结 2 1339
生来不讨喜
生来不讨喜 2021-02-04 14:56

Sorry for the possibly simple question. I\'m a programmer, though I rarely deal with graphics, and after tearing my hair out for hours with this problem, it\'s time to get some

2条回答
  •  日久生厌
    2021-02-04 15:54

    Two (less than ideal) options:

    #Use faceting, similar to Matthew's ggtitle option
    df = data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30))
    df$lab1 <- 'a'
    df$lab2 <- 'b'
    p = ggplot(df) + geom_point(aes(x = gp, y = y)) + facet_wrap(~lab1)
    p2 = ggplot(df) + geom_point(aes(x = y, y = gp)) + facet_wrap(~lab2)
    j <- theme(strip.text = element_text(hjust = 0.05))
    grid.arrange(p + j, p2 + j, ncol = 2)
    
    
    #Use grid.text
    grid.text(letters[1:2],x = c(0.09,0.59),y = 0.99)
    

    For the grid.text option, if you delve into the ggplot object you can probably avoid having to tinker to get those values right manually.

提交回复
热议问题