英文:
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 <- 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))
The resulting chart has much of empty white space around the actual plot. How can I reduce or crop this margin within R?
答案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 <- 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 to adjust as needed
答案2
得分: 1
将 scale_x_continuous
的 expand
参数设置为允许图表在不必每次重新缩放图表时更改边距的情况下填充空间:
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))
英文:
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 = "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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论