Reputation: 2269
The chart generated by this code does not place the chart in the center/middle of the transparent background of the output file. Is there a way to adjust the code so the circle ends up centered? I've fiddled with making different theme elements blank, to no avail. Thanks.
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)
Upvotes: 1
Views: 640
Reputation: 24838
You can use gtable_filter
from the gtable
package to extract just the plot panel:
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)
Upvotes: 1