减少 ggplot2 饼图周围的边距

标签 r ggplot2

我在文档中包含一个饼图,而其他图表在 ggplot2 中,因此我想使用 ggplot2 来保持图表的一致性。但是饼图没有延伸到边距

dat <- structure(list(y = c(0.0714285714285714, 
                            0.0714285714285714, 
                            0.107142857142857, 
                            0.160714285714286, 
                            0.25, 
                            0.339285714285714), 
                      x = structure(c(1L, 3L, 4L, 5L, 6L, 2L), 
                                    .Label = c("1 to 5", "6", "7", "8", "9", "10"), 
                                    class = "factor")), 
                 class = "data.frame", 
                 .Names = c("y", "x"), row.names = c(NA, -6L))

ggplot(dat, aes(x = factor(1), y = y, fill = factor(x))) + 
  geom_bar(width = 1, stat = "identity") + 
  coord_polar(theta  = "y") + 
  theme_grey()

我会使用 theme_void();我使用 theme_grey 来说明这一点:饼图的边缘在面板中延伸得不够远。如何使饼图的直径等于(或非常接近)面板的宽度?修改 width=1scale_x_discrete(expand = c(0,0)) 无效。 coord_polar 的参数似乎也没有提供任何线索。

enter image description here

最佳答案

这实际上并没有解决您的问题,但它确实将饼图的区域扩大到大约面板的初始大小。

ggplot(dat, aes(x = factor(1), y = y, fill = factor(x))) + 
geom_bar(width = 1, stat = "identity") + 
coord_polar(theta  = "y") + 
guides(fill=guide_legend(override.aes=list(colour=NA))) +
theme(panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      axis.text.x=element_blank(),
      plot.margin = unit(c(-.75,-.75,-.75,-.75),"cm")
      )

关于减少 ggplot2 饼图周围的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034560/

相关文章:

r - SAS PROC MIXED 与 lmerTest 输出

python - 使用 Seaborn/Matplotlib 的组条形图

r - R 和 ggplot 中有没有办法创建带有条件几何图形的facet_wrap 图?

r - 通过 ggplot2 中的列组合分面图

r - 如何绘制堆叠条形图,其中每个条形图的顺序基于列,每个级别的颜色基于另一个级别?

r - 在 R 中,是否有更好的方法来显示描述函数输出的描述性消息?

r - 无法使用 MonetDB 和 MonetDBLite 在 R 中组合大型调查数据帧

r - 设置种子后,该种子将对多长时间或多少代码块有效?

r - 具有可变字体系列的 geom_text 图例

r - 如何删除 R 中具有几乎相同列值的行?