简体   繁体   English

主标题和副标题在多面 ggplot2 中不正确 plot

[英]Main title and subtitle does not plot correctly in a faceted ggplot2

在此处输入图像描述 I am trying to plot a faceted gauge chart.我正在尝试 plot 多面量规图表。 The plot is OK except that the title and subtitle print in the first facet panel instead of the top. plot 没问题,只是标题和副标题打印在第一个方面面板而不是顶部。 How can I correct that, I have tried "inherit.aes = FALSE" but I am getting:我该如何纠正,我试过“inherit.aes = FALSE”,但我得到:

 unused argument (inherit.aes = FALSE)

Here is the data and code that I have tried.这是我尝试过的数据和代码。 Much thanks in advancef非常感谢提前f

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)
   )

The answer is simple: you are wraping the caption text to a width of 50 characters with str_wrap(width = 50) .答案很简单:您使用str_wrap(width = 50)将标题文本包装成 50 个字符的宽度。 Change 50 into a bigger number.50更改为更大的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM