将标题带到右上角

时间:2016-01-28 10:55:45

标签: r ggplot2

如何修改以下代码以将标题置于右上角。

value <- c(0, 1, 20, 3, 3, 0, 0, 5, 2, 5, 2, 7)
    names.arg =c("0-15","15-19","20-24","25-29","30-34",
                 "35-39","40-44","45- 49","50-54","55-59","60-64","65 Jahre oder Älter")
    df <- data.frame(names.arg = names.arg, value = value)

    p1 <- ggplot(df, aes(x=names.arg, y=value)) + geom_bar(stat = "identity")
    save(p1, file = "p1.png")

    value2 <- c(0, 1, 20, 3, 3, 0, 0, 5, 2, 5, 2, 7)
    names2 =c("0-15","15-19","20-24","25-29","30-34",
                 "35-39","40-44","45- 49","50-54","55-59","60-64","65 Jahre oder Älter")
    df2 <- data.frame(names = names2, value = value2)

    p2 <- ggplot(df2, aes(x=names, y=value)) + geom_bar(stat = "identity", fill = "red")
   title <- textGrob("Graph 1.2: plot", gp = gpar(fontsize=13,font=4))
   grid.arrange(p1,p2,nrow=2,top = title)

参考&#34; Error in grid.arrage - arrangeGrob() function&#34;

1 个答案:

答案 0 :(得分:1)

而不是gridExtra您可以使用包cowplot

#create grid
pgrid <- plot_grid(p1, p2, nrow=2)

#create title
title <- ggdraw() + draw_label("Graph 1.2: plot", hjust=-5, fontface=4, size=13)   

#combine plots and title
plot_grid(title, pgrid, ncol=1, rel_heights=c(0.1, 1))

enter image description here