减少ggplot2环形图中的空白空间。

huangapple go评论63阅读模式
英文:

Reduce empty space in ggplot2 donut chart

问题

我尝试了很多次,但在 Stack Overflow 上找不到对我的情况有用的答案...

使用以下代码,我在 R 中使用 ggplot2 包创建了一个环形图:

library(ggplot2)

test_data <- data.frame(Test = 1:4, Freq = c(0.1, 0.2, 0.3, 0.4))

ggplot(test_data, aes(x = 2, y = Freq, fill = Test)) +
  geom_bar(stat = "identity") +
  coord_polar(theta = "y", start = 0)+
  theme_void() +
  xlim(c(-1, 2.5)) +
  annotate(geom = "text", x = -1, y = 0, label = "Test", size = 60/.pt) +
  theme(legend.position = "none",
        plot.background = element_rect(color="red", linewidth = 2))

生成的图表周围有很多空白区域。我如何在 R 中减少或裁剪这个边距?

英文:

I tried hard, but could not find an answer on SO that helped in my case...

With the following code I create a donut chart in R using the ggplot2-package:

library(ggplot2)

test_data &lt;- data.frame(Test = 1:4, Freq = c(0.1, 0.2, 0.3, 0.4))

ggplot(test_data, aes(x = 2, y = Freq, fill = Test)) +
  geom_bar(stat = &quot;identity&quot;) +
  coord_polar(theta = &quot;y&quot;, start = 0)+
  theme_void() +
  xlim(c(-1, 2.5)) +
  annotate(geom = &quot;text&quot;, x = -1, y = 0, label = &quot;Test&quot;, size = 60/.pt) +
  theme(legend.position = &quot;none&quot;,
        plot.background = element_rect(color=&quot;red&quot;, linewidth = 2))

The resulting chart has much of empty white space around the actual plot. How can I reduce or crop this margin within R?

减少ggplot2环形图中的空白空间。

答案1

得分: 3

尝试使用theme中的plot.margin来根据需要进行调整。

library(ggplot2)

test_data <- data.frame(Test = 1:4, Freq = c(0.1, 0.2, 0.3, 0.4))

ggplot(test_data, aes(x = 2, y = Freq, fill = Test)) +
  geom_bar(stat = "identity") +
  coord_polar(theta = "y", start = 0) +
  theme_void() +
  xlim(c(-1, 2.5)) +
  annotate(geom = "text", x = -1, y = 0, label = "Test", size = 60/.pt) +
  theme(legend.position = "none",
        plot.background = element_rect(color = "red", linewidth = 2),
        plot.margin = margin(-1, -1, -1, -1, "cm")) # 根据需要进行调整
英文:

Try use plot.margin within theme to adjust as needed.

library(ggplot2)

test_data &lt;- data.frame(Test = 1:4, Freq = c(0.1, 0.2, 0.3, 0.4))

ggplot(test_data, aes(x = 2, y = Freq, fill = Test)) +
  geom_bar(stat = &quot;identity&quot;) +
  coord_polar(theta = &quot;y&quot;, start = 0) +
  theme_void() +
  xlim(c(-1, 2.5)) +
  annotate(geom = &quot;text&quot;, x = -1, y = 0, label = &quot;Test&quot;, size = 60/.pt) +
  theme(legend.position = &quot;none&quot;,
        plot.background = element_rect(color = &quot;red&quot;, linewidth = 2),
        plot.margin = margin(-1, -1, -1, -1, &quot;cm&quot;)) # Try to adjust as needed

答案2

得分: 1

scale_x_continuousexpand 参数设置为允许图表在不必每次重新缩放图表时更改边距的情况下填充空间:

ggplot(test_data, aes(x = 2, y = Freq, fill = Test)) +
  geom_bar(stat = "identity") +
  coord_polar(theta = "y", start = 0) +
  scale_x_continuous(expand = c(0, -0.6), limits = c(-1, 2.5)) +
  theme_void() +
  annotate(geom = "text", x = -1, y = 0, label = "Test", size = 60/.pt) +
  theme(legend.position = "none",
        plot.background = element_rect(color="red", linewidth = 2))

减少ggplot2环形图中的空白空间。

英文:

Setting the expand argument of scale_x_continuous will allow the plot to fill the space without having to change the margin every time you rescale the plot:

ggplot(test_data, aes(x = 2, y = Freq, fill = Test)) +
  geom_bar(stat = &quot;identity&quot;) +
  coord_polar(theta = &quot;y&quot;, start = 0) +
  scale_x_continuous(expand = c(0, -0.6), limits = c(-1, 2.5)) +
  theme_void() +
  annotate(geom = &quot;text&quot;, x = -1, y = 0, label = &quot;Test&quot;, size = 60/.pt) +
  theme(legend.position = &quot;none&quot;,
        plot.background = element_rect(color=&quot;red&quot;, linewidth = 2))

减少ggplot2环形图中的空白空间。

huangapple
  • 本文由 发表于 2023年6月22日 20:39:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76532019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定