Home > database >  Main title and subtitle does not plot correctly in a faceted ggplot2
Main title and subtitle does not plot correctly in a faceted ggplot2

Time:09-04

enter image description hereenter image description hereI am trying to plot a faceted gauge chart. The plot is OK except that the title and subtitle print in the first facet panel instead of the top. How can I correct that, I have tried "inherit.aes = FALSE" but I am getting:

 unused argument (inherit.aes = FALSE)

Here is the data and code that I have tried. Much thanks in advancef

library(showtext) 
library(tidyverse)


# add fonts

font_add_google(name = "Bebas Neue", family = "Bebas Neue")
showtext_auto()

#data 
 df <- data.frame(matrix(nrow=5, ncol = 2))

names(df) <- c("variable", "percentage")
df$variable <- c("Carbohydrates", "Warming", "NGTnotPresent", "DrainNotPresent", "DrEaMing")
df$percentage <- c(0.67,0.33,0.86,0.78,0.58)

df <- df %>% mutate(group=ifelse(percentage <0.6, "red",
 ifelse(percentage>=0.6 & percentage<0.8, "orange","green")),
 label=paste0(percentage*100, "%"),
 title=dplyr::recode(variable, `Carbohydrates`="Preoperative\ncarbohydrate loading",
 `Warming`="Intraoperative\nwarming",
 `NGTnotPresent`="\nPatients without a\nnasogastric tube\non arrival in recovery",
 `DrainNotPresent`="\nPatients without an\nabdominal drain\non arrival in recovery",
 `DrEaMing`="Patients DrEaMing on\npostoperative day 1"))

title <- "pomVLAD risk-adjustment model"
subtitle <- 'The model is being used to produce a variable life-adjusted display plot for each of the 10 pilot hospitals showing their expected against observed postoperative morbidity outcomes' %>% 
      str_wrap(width = 50)
caption <- "Data: pomVLAD | Viz: @stepminer2"

#plot 
gg<- ggplot(df, aes(fill = group, ymax = percentage, ymin = 0, xmax = 2, xmin = 1))  
 geom_rect(aes(ymax=1, ymin=0, xmax=2, xmin=1), fill ="#ece8bd")  
 geom_rect()   
 coord_polar(theta = "y",start=-pi/2)   xlim(c(0, 2))   ylim(c(0,2))  
 geom_text(aes(x = 0, y = 0, label = label, colour=group), size=25.5, family=""Bebas Neuel"", face= "bold")  
 geom_text(aes(x=1.5, y=1.5, label=title), family=""Bebas Neuel"", size=14.2, color="black")   
 facet_wrap(~title, ncol = 5)  
 theme_void()  
 scale_fill_manual(values = c("red"="#C9146C", "orange"="#DA9112", "green"="#129188"))  
 scale_colour_manual(values = c("red"="#C9146C", "orange"="#DA9112", "green"="#129188"))  
 theme(strip.background = element_blank(),
 strip.text.x = element_blank()
 
 )  
 guides(fill=FALSE)  
 guides(colour=FALSE)


gg 
 labs(
  title = title,
  subtitle = subtitle,
  caption = caption) 
 theme(
  plot.title = element_text(family = "Bebas Neue", size = 50, colour = "black",
                            margin = margin(t = 20, b = 10),  inherit.aes = FALSE),
  plot.subtitle = element_text(family = "Bebas Neue", size = 30, colour = "black",
                               margin = margin(b = 20), inherit.aes = FALSE),
  plot.caption = element_text(color = "black", size = 20, hjust = 0.5)
   )

CodePudding user response:

The answer is simple: you are wraping the caption text to a width of 50 characters with str_wrap(width = 50). Change 50 into a bigger number.

  • Related