我正在生成一个饼图,我想将其合并到一个Shiny页面中。绘图区域(即:图中的白色空间)将是流动的。我希望绘图本身(灰色区域和饼图)能够对齐绘图区域的左侧,而不是显示居中。有什么想法吗?
ggplot(treeData, aes(x = "", fill = ClassName)) +
ggtitle("Species distribution") +
geom_bar(width = 1) +
coord_polar("y", start=0) +
xlab("") + ylab("") +
theme(
axis.text.x=element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
plot.margin=unit(c(25,0,0,0), "pt")) +
scale_fill_manual(values= c("#ffff00", "#008080", "#00ff00"))
答案 0 :(得分:1)
提供了有关此答案工作原理的更详细解释here.简而言之,您需要将绘图放置在可以在调整封闭图像大小时展开的网格中。
library(ggplot2)
library(grid)
library(gtable)
# some test data
animals <- as.data.frame(
table(Species =
c(rep("Moose", sample(1:100, 1)),
rep("Frog", sample(1:100, 1)),
rep("Dragonfly", sample(1:100, 1))
)))
# make the pie chart
g <- ggplot(animals, aes(x = "", y = Freq, fill = Species)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) +
theme(
axis.text.x=element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
plot.margin=unit(c(25,0,0,0), "pt"))
# set the desired width and height of the
# pie chart (need to play around to find
# numbers that work
plotwidth = unit(6.1, "inch")
plotheight = unit(5, "inch")
# place into matrix that can expand
grob <- ggplotGrob(g)
mat <- matrix(list(grob, nullGrob(), nullGrob(), nullGrob()), nrow = 2)
widths <- unit(c(1, 1), "null")
widths[1] <- plotwidth
heights <- unit(c(1, 1), "null")
heights[1] <- plotheight
gm <- gtable_matrix(NULL, mat, widths, heights)
grid.newpage()
grid.draw(gm)
答案 1 :(得分:0)
您可以尝试添加
来更改边距theme(plot.margin = unit(c(5.5, 100, 5.5, -100), "points"))
或使用cowplot功能
cowplot::plot_grid(your_plot, NULL, ncol=2, rel_widths = c(2,1))