ggplot-为主题添加自定义字体

时间:2019-09-13 09:47:14

标签: r ggplot2

我为ggplot创建了一个主题,如下所示:

my_theme <- function() {

  theme_minimal() %+replace%
    theme(


      plot.margin = unit(c(1, 1, 1, 0.5), "cm"),
      plot.title = element_text(size = 14, face = "bold", hjust = 0.5, colour = "#DBD9D9", margin = margin(0,0,10,0)),
      plot.subtitle = element_text(size = 12, hjust = 0.5, colour = "#797B86", margin=margin(0,0,30,0)),
      plot.background = element_rect(fill = "#2D3142"),

      panel.border = element_rect(colour = "#2D3142", fill=NA, size=1),
      panel.grid.major.x = element_blank(),
      panel.grid.major = element_line(colour = "#586082", size = 0.1, linetype = "dotted"),
      panel.grid.minor = element_line(colour = "#586082", size = 0.3, linetype = "dotted"),
      panel.background = element_rect(fill = '#2D3142'),

      axis.line = element_line(colour = "#2D3142"),
      axis.title = element_text(colour = "#797B86", size = 11),
      axis.text = element_text(colour = "#DBD9D9", size = 9, face = "bold"),
      axis.title.x = element_text(margin = margin(0.5, 0, 1, 0, unit = "cm")),
      axis.title.y = element_text(margin = margin(0, 0.5, 0, 0, unit = "cm")),

      legend.margin = margin(unit(c(3, 3, 3, 3), "cm")),
      legend.position = "bottom",
      legend.background = element_rect(fill = "#2D3142", color = NA),
      legend.text = element_text(colour = "#DBD9D9", size = 11, face = "bold"),
      legend.title = element_text(colour = "#797B86", size = 11),
      legend.key = element_blank(),

      complete = TRUE
    )
}

这很有效,直到我尝试添加自定义字体后才这样

with_font <- function() {
  windowsFonts(lucida = windowsFont("Lucida Console"))

  theme_minimal() %+replace%
    theme(
      text = element_text(family = "lucida"),

      plot.margin = unit(c(1, 1, 1, 0.5), "cm"),
      plot.title = element_text(size = 14, face = "bold", hjust = 0.5, colour = "#DBD9D9", margin = margin(0,0,10,0)),
      plot.subtitle = element_text(size = 12, hjust = 0.5, colour = "#797B86", margin=margin(0,0,30,0)),
      plot.background = element_rect(fill = "#2D3142"),

      panel.border = element_rect(colour = "#2D3142", fill=NA, size=1),
      panel.grid.major.x = element_blank(),
      panel.grid.major = element_line(colour = "#586082", size = 0.1, linetype = "dotted"),
      panel.grid.minor = element_line(colour = "#586082", size = 0.3, linetype = "dotted"),
      panel.background = element_rect(fill = '#2D3142'),

      axis.line = element_line(colour = "#2D3142"),
      axis.title = element_text(colour = "#797B86", size = 11),
      axis.text = element_text(colour = "#DBD9D9", size = 9, face = "bold"),
      axis.title.x = element_text(margin = margin(0.5, 0, 1, 0, unit = "cm")),
      axis.title.y = element_text(margin = margin(0, 0.5, 0, 0, unit = "cm")),

      legend.margin = margin(unit(c(3, 3, 3, 3), "cm")),
      legend.position = "bottom",
      legend.background = element_rect(fill = "#2D3142", color = NA),
      legend.text = element_text(colour = "#DBD9D9", size = 11, face = "bold"),
      legend.title = element_text(colour = "#797B86", size = 11),
      legend.key = element_blank(),

      complete = TRUE
    )
}

我总是会出错

  

FUN(X [[i]],...)中的错误:主题元素“文本”具有NULL属性:   面,颜色,大小,对齐,垂直,角度,线高,边距,调试

一旦我尝试在情节中使用它。你们能建议我这怎么了吗?

0 个答案:

没有答案