r - 如何减少ggplot绘图区域中的空白?

标签 r ggplot2

我有一个饼图。我想减少面板中的空白区域。我将背景颜色设为绿色,以便您查看空白区域所占的区域。

这是我的数据

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

piegraph 我应该更新哪一部分以使空白区域缩小?我喜欢我的图例和标题,以更接近馅饼。

最佳答案

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/

相关文章:

r - data.table::fread 不喜欢第一列中的缺失值

r - 删除数据框中引用另一个 (R) 中不存在的 ID 的行?

r - 将文字描述放在 kable 旁边

r - 在 Windows 上托管 Shiny 的应用程序

R:一起绘制箱线图和函数图

r - 使用 ggplot2 绘制 POSIXct 时间戳系列

r - 将参数传递给与显式参数冲突的点

r - 创建自定义几何图形来计算汇总统计数据并在绘图区域*外部*显示它们

r - 使用 geom_bar 的 ggplotly 缺少工具提示和冗余图例

r - ggplot2 跨组中的嵌套方面