0 votes
1 view
in R Programming by (5.3k points)

I'm using ggplot2 to improve precipitation bar plots.

Here's a reproducible example of what I want to achieve:

library(ggplot2)

library(gridExtra)

secu <- seq(1, 16, by=2)

melt.d <- data.frame(y=secu, x=LETTERS[1:8])

m <- ggplot(melt.d, aes(x=x, y=y)) +

  geom_bar(fill="darkblue") + 

  labs(x="Weather    stations", y="Accumulated Rainfall [mm]") +

  opts(axis.text.x=theme_text(angle=-45, hjust=0, vjust=1),

       title=expression("Rainfall"), plot.margin = unit(c(1.5, 1, 1, 1), "cm"),

       plot.title = theme_text(size = 25, face = "bold", colour = "black", vjust = 5))

z <- arrangeGrob(m, sub = textGrob("Location", x = 0, hjust = -3.5, vjust = -33, gp = gpar(fontsize = 18, col = "gray40"))) #Or guessing x and y with just option

z

I don't know how to avoid using guessing numbers on hjust and vjust on ggplot2? Is there a better way to put a subtitle (not just using \n, but a subtitle with different text color and size)?

I need to be able to use with ggsave to have a pdf file.

1 Answer

0 votes
by (25.3k points)

To add a ggplot2 subtitle with different size and color, you can use the plot.subtitle argument in the theme function as follows:

library(ggplot2) 

secu <- seq(1, 16, by=2)

melt.d <- data.frame(y=secu, x=LETTERS[1:8])

m <-  ggplot(melt.d, aes(x=x, y=y))+ 

            geom_bar(fill="darkblue", stat="identity")+

            labs(x="Weather    stations", 

                 y="Accumulated Rainfall [mm]",

                 title="Rainfall",

                  subtitle="Location")

m <- m + theme(plot.title=element_text(size=25, hjust=0.5, face="bold", colour="maroon", vjust=-1))

m <- m + theme(plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="black"))

m

Output:

image

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...