shoorideh
shoorideh

Reputation: 173

Bring title to the top right corner

How can I Modify the below code to bring the title to the top right corner.

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)

Reference "Error in grid.arrage - arrangeGrob() function"

Upvotes: 0

Views: 181

Answers (1)

erc
erc

Reputation: 10123

Instead of gridExtra you could use the package 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

Upvotes: 1

Related Questions