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

后端 未结 2 1338
生来不讨喜
生来不讨喜 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:32

    You could use ggtitle and theme:

    df = data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30))
    p = ggplot(df) + geom_point(aes(x = gp, y = y)) + ggtitle('a') + theme(plot.title=element_text(hjust=0))
    p2 = ggplot(df) + geom_point(aes(x = y, y = gp)) + ggtitle('b') + theme(plot.title=element_text(hjust=0))
    grid.arrange(p, p2, ncol = 2)
    

    enter image description here

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题