我有一个饼图。我想减少面板中的空白区域。我将背景颜色设为绿色,以便您查看空白区域所占的区域。
这是我的数据
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)
“顶部”
。
希望对你有帮助
关于r - 如何减少ggplot绘图区域中的空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57662688/