Ggplot multi-line header with different font size, face, etc.

I looked at SO and other sites, but cannot find a solution to my problem using ggtitle(). I know that there are other examples, but I could not apply them directly to my problem successfully.

What I'm trying to do is make a multi-line plot title, where the first line is in bold, size = 10, and then below this first line is the second (and potentially third), more descriptive line (s) -larger, size = 8 Kicker - this is what I'm trying to do it left along the Y axis. This left excuse is what makes this question unique, because previous answers, including those related to the moderator, use atop()one that doesn't allow left excuse or doesn't include it in response related.

Here's what my plot looks like:

enter image description here

title <- paste("An analysis of the mtcars dataset  ")
subheader <- paste("How does mpg change by   \n number of cyl?")

ggplot(mtcars, aes(mpg,cyl))+ 
  geom_smooth(aes(fill="Mean",level=0.095)) +
  ggtitle(paste0(title,"\n",subheader)) +
  scale_fill_grey(name='95% Confidence\n Interval',start=.65,end=.65) +
  theme(plot.title = element_text(size = rel(2.0),hjust=-.1,face="bold"))

I tried to use the bquote(), mtext(), atop()and even grungy paste()with additional spaces included pushing for the title .... but I could not find a solution.

, , - - . !

+4
3

. , . , , .

library(grid) 

title <- paste("An analysis of the mtcars dataset  ")
subheader <- paste("How does mpg change by\nnumber of cyl?")

p1 = ggplot(mtcars, aes(mpg,cyl))+ 
  geom_smooth(aes(fill="Mean",level=0.095)) +
  scale_fill_grey(name='95% Confidence\n Interval',start=.65,end=.65) 

p1 = p1 + 
  annotation_custom(grob=textGrob(title, just="left", 
                                  gp=gpar(fontsize=10, fontface="bold")),
                    xmin=9.8, xmax=9.8, ymin=11.7) +
  annotation_custom(grob=textGrob(subheader, just="left", 
                                  gp=gpar(fontsize=8, lineheight=1)),
                    xmin=9.8, xmax=9.8, ymin=10.4) +
  theme(plot.margin=unit(c(4,rep(0.5,3)), "lines"))

# Turn off clipping
p1 <- ggplot_gtable(ggplot_build(p1))
p1$layout$clip[p1$layout$name == "panel"] <- "off"
grid.draw(p1) 

enter image description here

+3

, tableGrob ,

enter image description here

title <- paste("An analysis of the mtcars dataset  ")
subheader <- paste("How does mpg change by \nnumber of cyl?")

library(gridExtra)
library(grid)

table_label <- function(label, params=list())  {

  params1 <- modifyList(list(hjust=0, x=0, fontsize=12, fontface=2), params)
  params2 <- modifyList(list(hjust=0, x=0, fontsize=8, fontface=1), params)

  mytheme <- ttheme_minimal(core = list(fg_params = params2),
                            colhead = list(fg_params = params1))
  disect <- strsplit(label, "\\n")[[1]]
  m <- as.matrix(disect[-1])
  g <- tableGrob(m, cols=disect[1], theme=mytheme)
  g$widths <- unit(1,"npc")
  g
}


p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
      geom_line() 

## add a title, note: centering is defined wrt the whole plot
grid.arrange(p, top=table_label(paste0(title,"\n",subheader), 
                                params=list(x=0.1)))

## to align precisely with the panel, use gtable instead
library(gtable)
titleify <- function(p, label, ...){
  g <- ggplotGrob(p)
  title <- table_label(label, ...)
  g <- gtable_add_grob(g, title, t=1, l=4)
  g$heights <- grid:::unit.list(g$heights)
  g$heights[1] <-list(sum(title$heights))
  grid.newpage()
  grid.draw(g)
}

titleify(p, paste0(title,"\n",subheader))

## we can also hack ggplot2 to define a custom element for the title
## though be warned the hardware supporting your computer may be damaged by head banging

element_custom <- function() {
  structure(list(), class = c("element_custom", "element_text"))
}

element_grob.element_custom <- function(element, label="", ...)  {
  table_label(label)
}

# default method is unreliable
heightDetails.gtable <- function(x) sum(x$heights)

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_line() + 
  ggtitle(paste0(title,"\n",subheader))+
  (theme_grey() %+replace% theme(plot.title = element_custom()))
+4

, . atop , , , hjust. , , ; .

:

library(ggplot2)
title <- paste("           An analysis of the mtcars dataset")
subheader <- paste("How does mpg change by number of cyl?")

ggplot(mtcars, aes(mpg,cyl)) + 
  geom_smooth(aes(fill="Mean",level=0.095)) +
  ggtitle(bquote(atop(bold(.(title)), atop(.(subheader), ""))))  +
  scale_fill_grey(name='95% Confidence\n Interval',start=.65,end=.65) +
  theme(plot.title = element_text(size = rel(1.3),hjust=-.6,face="bold"))

plot with a headline and subtitle on the left

, , , grid.

+2

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


All Articles