首页 > 解决方案 > 如何减少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

图形 为了缩小空白,我应该更新哪个部分?我喜欢我的图例和标题,以便更接近馅饼。

标签: rggplot2

解决方案


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".

希望这有帮助


推荐阅读