r - 如何减少ggplot绘图区域中的空白?
问题描述
我有一个饼图。我想减少面板中的空白。我将背景设置为绿色,以便您查看空白区域所占据的区域。
这是我的数据
structure(list(PER = c(34.4413912651734, 64.2857433622907, 1.2728653725359
), CAT = structure(1:3, .Label = c("Yes", "No", "Don’t know/Refused"
), class = "factor")), row.names = c("Yes", "No", "Don’t know/Not sure"
), class = "data.frame")
这是我到目前为止所尝试的:
xfall <- ggplot(table_fall_FRQ, aes(x="", y=PER, fill=CAT)) +
geom_bar(width = 1, size = 1, stat = "identity") +
coord_polar("y") +
geom_text(aes(label = paste0(round(PER), "%")),
position = position_stack(vjust = 0.5), color="white",
size=14) +
labs(#title="",
#subtitle="",
caption="n= respondents with income less than $60,000 year \nSource: Sample",
x="",
y="") +
scale_fill_viridis(discrete=TRUE) +
guides(fill = guide_legend(reverse = FALSE)) +
theme_classic() +
theme(axis.text.x = element_blank(),
axis.line.x = element_blank(),
axis.ticks.x = element_blank(),
axis.line = element_blank(),
axis.text=element_text(size=14, color="white"),
#axis.title=element_text(size=14,face="bold"),
#panel.grid.major = element_blank(),
panel.spacing.x=unit(0, "lines"),
panel.spacing.y=unit(0, "lines"),
legend.position="top",
legend.box.spacing = unit(0.1, 'cm'),
legend.margin = margin(0, 0, 0, 0, "cm"),
legend.title = element_blank(),
plot.caption = element_text(hjust = 0, lineheight=1.5),
#plot.subtitle = element_text(color="blue"),
plot.background = element_rect(fill="green"),
plot.margin = margin(0, 0, 0, 0, "cm")
)
xfall
解决方案
Rui的帖子帮我找到了。
xfall <- ggplot(table_fall_FRQ, aes(x="", y=PER, fill=CAT)) +
geom_bar(width = 1, size = 1, stat = "identity") +
coord_polar("y") +
geom_text(aes(label = paste0(round(PER), "%")),
position = position_stack(vjust = 0.5), color="white",
size=14) +
labs(#title="",
#subtitle="",
caption="n= respondents with income less than $60,000 year \nSource: Sample",
x="",
y="") +
scale_fill_viridis(discrete=TRUE) +
guides(fill = guide_legend(reverse = FALSE)) +
theme_classic() +
theme(axis.text = element_blank(),
axis.ticks.length = unit(0, "mm"),
axis.line = element_blank(),
#axis.title=element_text(size=14,face="bold"),
#panel.grid.major = element_blank(),
panel.spacing.x=unit(0, "lines"),
panel.spacing.y=unit(0, "lines"),
legend.position="top",
legend.box.spacing = unit(-1, 'cm'),
legend.margin = margin(0, 0, 0, 0, "cm"),
legend.title = element_blank(),
plot.caption = element_text(hjust = 0,margin = unit(c(-15,0,0,0), "mm")),
#plot.subtitle = element_text(color="blue"),
plot.background = element_rect(fill="green"),
plot.margin = margin(0, 0, 0, 0, "cm")
)
xfall
您可以通过使用向量来进一步更改标题plot.caption
的位置,并使用向量来更改图例的位置,例如c(0.5, 0.5)
代替"top"
.
希望这有帮助
推荐阅读
- c# - 有没有检测 SignalR-Core Hub 死锁和线程挂起的好工具?
- angular - 如何从Angular Form中的动态绑定(多次)元素中删除验证?
- angular - 如何使用递归组件编译 Angular 12 库?
- flutter - 插件 `libphonenumber` 使用了已弃用的 Android 嵌入颤振版本
- php - 选择字段中最后一行的条件为真
- c++ - 即使使用 -std=c++2a 和 g++-11 也无法使用 std::chrono::parse
- android - androidx proxyconfig如何设置用户密码认证
- python - 我可以从一项服务购买域名并在另一项服务上托管 Web 应用程序吗?
- textarea - 如何使用 RedActorX 在 textarea 中设置字符串限制
- java - 推荐用于两个相同大小的表格和按钮的 Java Swing 布局