r - 在其透明背景上居中饼图
问题描述
此代码生成的图表不会将图表放置在输出文件透明背景的中心/中间。有没有办法调整代码使圆最终居中?我一直在摆弄不同的主题元素,但无济于事。谢谢。
library(ggplot2)
# sample data
df <- data.frame(group = factor(c("A", "B", "C")), y = c(20,30,50))
# plot code
pie <- ggplot(df, aes(x="", y= y, fill=group))+
geom_bar(width = 1, stat = "identity", show.legend = FALSE)+
xlab("")+
ylab("")+
coord_polar("y", start=0)+
theme_minimal()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
# save as png with transparent background
ggsave(filename= "pie.png",
plot= pie,
device = "png",
type = "cairo-png",
bg = "transparent",
width = 2,
height = 2,
units = "cm",
dpi = 800)
解决方案
您可以gtable_filter
从gtable
包中使用仅提取绘图面板:
library(gtable)
pie <- ggplot(df, aes(x="", y= y, fill=group))+
geom_bar(width = 1, stat = "identity", show.legend = FALSE)+
xlab("")+
ylab("")+
coord_polar("y", start=0)+
expand_limits(y = 0) +
theme_minimal()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
pie <- ggplotGrob(pie)
pie <- gtable::gtable_filter(pie, "panel")
ggsave(filename= "pie.png",
plot= pie,
device = "png",
bg = "transparent",
width = 2,
height = 2,
units = "cm",
dpi = 800)
推荐阅读
- mysql - 如何在 jOOQ for MySQL 8 中实现 JSON_SET()
- java - 在Android中将片段显示为BottomSheet
- sql - NOT 条件不包括 NULL 值
- javascript - jquery如何从文本框中获取值
- xml - 隐藏自定义(/)页面的标题odoo v12
- android - 向不同客户发布/推出同一应用程序的不同版本/定制的选项?
- c++ - Boost intrusive list 中有很多编译错误
- r - 分析 R 中的数据,我想将图表导出到 powerpoint
- mono - 如何结合 Mono 和 Flux 作为参数来创建新的 Mono?
- google-chrome - 如何在 Chrome 74+ 中禁用 CORB