Adjust the title vertically inside the plot - vjust does not work

I want to put the title inside the plot, and not at the top position by default. Here is a simple code snippet

library(ggplot2)
df <- data.frame(x = c(1:10), y = rnorm(10, 1, 2))
ggplot(df, aes(x, y))+
   geom_line() +
   ggtitle("Demo") + 
   theme(plot.title = element_text(vjust = -3)) 

In the past, I was able to do this by changing the value vjust, but now it does not work. Any idea how to do this?

+4
source share
1 answer

The ggplot problem "does vjust not work in v 2.0 for plot.title?" Hadley writes:

" , . , . , vjust hjust . , margin() element_text() "

t b margin, , :

ggplot(df, aes(x, y))+
  geom_line() +
  ggtitle("Demo") + 
    theme(plot.title = element_text(margin = margin(t = 10, b = -20)))

enter image description here

. ?margin.


, margin axis.title.x axis.title.y:

ggplot() + ggtitle("this is title") + xlab("this is x") + ylab("this is y") + 
  theme(plot.title = element_text(margin = margin(b = -10)),
        axis.title.x = element_text(margin = margin(t = -10)),
        axis.title.y = element_text(margin = margin(r = -10)))

enter image description here

+7

Source: https://habr.com/ru/post/1624387/


All Articles