我需要我的图表有一个深灰色主题来匹配演示风格。我还需要它们的高度是固定的,但宽度可能会根据 y 轴上标签的长度而有所不同。当我尝试导出或保存时,.jpg 或 .png 文件中总是有白色的边栏。
这是我用来创建图表的一些示例代码(此处有一些额外的主题控件对于简化示例来说是多余的,但生成的图表基本上就是我生成的):
library(ggplot2)
bar.font <- 8
title <- "Example"
l_labs <- c("")
x_labs <- c("A","B","C")
ests <- c(.5,.3,.2)
nerrs <- c(.05, .05, .05)
perrs <- nerrs
barchart.data <- data.frame(l_labs, x_labs, ests, nerrs, perrs)
p <- ggplot(barchart.data, aes(x=x_labs, y=ests*100)) +
geom_bar(stat="identity", color="#808080", position=position_dodge(), width=0.85, fill="#808080") +
geom_text(aes(y=ests*100+perrs*100+1.5, label=sprintf("%1.1f%%", 100*ests)), vjust=0.5, hjust=0, size=bar.font, color="white") +
geom_errorbar(aes(ymin=ests*100-nerrs*100, ymax=ests*100+perrs*100), width=.2, position=position_dodge(.9), color="white", size=0.25) +
labs(title=title, x="", y = "") + theme_classic() +
scale_y_continuous(expand = c(0,0),limits = c(0,115), breaks=c(0, 20, 40, 60, 80, 100)) +
theme(legend.position="none", legend.text = element_text(color = "white")) +
theme(title = element_text(size=18, colour = "white")) +
theme(axis.text = element_text(size=20, color = "white"), axis.line = element_line(color = "white")) +
theme(axis.text.x = element_text(margin=margin(9,0,0,0)),axis.text.y = element_text(margin=margin(0,9,0,0))) +
theme(axis.title = element_text(size=20, color = "white")) +
theme(axis.title.x = element_text(margin = margin(10,0,0,0))) +
theme(axis.ticks = element_line(colour = 'white', size = .5)) +
coord_flip() +
theme(aspect.ratio = 1) +
theme(panel.background = element_rect(fill = "#1e1e1e")) +
theme(legend.justification=c(1,0), legend.position=c(1,0)) +
theme(plot.background = element_rect(fill = "#1e1e1e", color = "#1e1e1e")) +
theme(panel.grid.major.x = element_line(colour = "white",size=0.1, linetype = "dotted"))
ggsave("test.jpg", height=10, units="in")
Run Code Online (Sandbox Code Playgroud)
这是导出的 .jpg 的样子。我无法指定确切的宽度,因为我不知道每个图表的宽度会有所不同。感谢您的任何指导。

您可以将背景颜色设置为您喜欢的任何值:
ggsave("test.jpg", height=10, units="in", bg = "#1e1e1e")
Run Code Online (Sandbox Code Playgroud)
这会处理白条。
文档中bg没有提到该选项可能有点令人困惑ggsave()。那是因为它是图形设备的一部分,在这里jpeg()。它在jpeg()文档中提到。
| 归档时间: |
|
| 查看次数: |
685 次 |
| 最近记录: |