我正在尝试创建ggplot2绘图样式。 theme_custom()
函数处理调整后的主题。困难的部分是覆盖title
,subtitle
和caption
的边距的部分。我使用finalise_plot()
函数的目标是将文本对齐到图的最左侧。
直到我偶然发现出口问题时,该图看起来几乎就像我想要的那样。当我尝试将图像(通过绘图设备)保存为pdf时,它将删除所有文本。当我将其导出为svg时,它已损坏。
是否可以像下面的示例那样在svg中导出确切的输出?
library(tidyverse)
library(grid)
library(gridExtra)
theme_custom <- function(){
theme_minimal() +
theme(plot.title = element_text(face = "bold",
family = "Arial",
size = 24),
plot.subtitle = element_text(face = "bold",
family = "Arial",
size = 20),
plot.caption = element_text(hjust = 0,
color = "#6c737b",
size = 18,
family = "Arial"),
axis.text = element_text(size = 18,
color = "#1d2939",
family = "Arial"),
axis.title.y = element_text(color = "#6b38e8",
family = "Arial",
size = 18,
hjust = 0),
axis.text.y = element_text(color = "#6b38e8"),
plot.background = element_rect(fill = "#f2f2f2",
color = 0),
panel.grid.minor = element_blank(),
panel.grid = element_line(color = "#d8cedb",
size = 0.75),
panel.border = element_blank(),
plot.margin = unit(c(10,0,0,0),"pt"))
}
finalise_plot <- function() {
table_grob <- ggplotGrob(p)
table_grob$layout$l[table_grob$layout$name %in% c("title", "subtitle", "caption")] <- 1
grid::grid.draw(table_grob)
}
p <- iris %>%
ggplot(aes(Sepal.Length, Sepal.Width,
color = Species)) +
geom_point() +
theme_custom() +
labs(title = "Main title",
subtitle = "Subtitle",
caption = "UCI Machine Learning Repository",
x = NULL)
finalise_plot()
由reprex package(v0.2.1)于2019-04-16创建
svg导出: