R ggplot - 将标题放在饼图底部[重复]

问题描述 投票:0回答:1

一直在 ggplot2 中制作饼图。到目前为止我有这个代码:

library(tidyverse)    
pie_tbl <- tibble(
  surface = factor(c("Dry","Wet","Snow/Ice","Other"),levels=c("Dry","Wet","Snow/Ice","Other")),
  percentage = c(78,14.8,6.9,0.2)
)


pie_tbl %>% ggplot(aes(x="",y=percentage,fill=surface)) + 
  geom_col(color='black',width=1) + coord_polar("y",start=pi/2) +
  labs(title="Surface Condition during collisions") +
  theme_void() +
  theme(plot.title = element_text(family='serif',face='bold',size=18,hjust=0.5,vjust=-1)) +
  scale_fill_manual(
    values=c(
      "Dry" = "#27B7CE",
      "Wet" = "#FB6476",
      "Snow/Ice" = "#47B972",
      "Other" = "#FEB20E"
      )
    )
)

我明白了这个情节 饼图

vjust = -1 不会将标题移至底部。有什么想法吗?

我发现 vjust 工作的负值很大(例如 -100),但这感觉像是一个“hacky”解决方案,并且可能有更好的方法。

r ggplot2 pie-chart
1个回答
1
投票

一个技巧可能是使用“标题”来代替:

labs(caption="Surface Condition during collisions") +
theme(plot.caption = element_text(size = 16, hjust = 0.5, vjust = 2)) +

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.