绘图标题在 Circular Barplot 中不起作用

Plot title not working in Circular Barplot

我正在尝试使用 ggtitle() 向我的圆形条形图添加标题,但在图中的任何地方都找不到标题。我什至尝试调整位置但没有用。有人可以告诉我这个问题吗? 示例输入和代码如下:

grp = c("A","B","C","D","E","F","G","H","I","J")
val= c(104,95,73,65,53,43,30,20,10,5)
data= data.frame(grp,val, stringsAsFactors = TRUE)

ggplot(data, aes(x=grp, y=val)) +       
  geom_bar(stat="identity", fill=alpha("blue", 0.3)) +
  ylim(-100,120) +
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-2,4), "cm")     
  ) +
  coord_polar(start = 0) +
  geom_text(aes(x=grp, y=val, label=val+10, vjust=-0.5), color="black", fontface="bold",alpha=0.6, size=2.5, inherit.aes = FALSE ) +
  geom_text(aes(x=grp, y=val, label=grp, hjust=2), color="black", fontface="bold",alpha=0.6, size=2.5, inherit.aes = FALSE ) +
  ggtitle("Title is top 10")+
  theme(plot.title = element_text(hjust = 0.5))

问题是您将图边距设置为负值 2 厘米。距离绘图边距或边距小于 2 厘米的所有内容,例如标题将被“截断”或至少不显示。要解决此问题,还要调整标题的边距:

library(ggplot2)

ggplot(data, aes(x=grp, y=val)) +       
  geom_bar(stat="identity", fill=alpha("blue", 0.3)) +
  ylim(-100,120) +
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-2,4), "cm")     
  ) +
  coord_polar(start = 0) +
  geom_text(aes(x=grp, y=val, label=val+10, vjust=-0.5), color="black", fontface="bold",alpha=0.6, size=2.5, inherit.aes = FALSE ) +
  geom_text(aes(x=grp, y=val, label=grp, hjust=2), color="black", fontface="bold",alpha=0.6, size=2.5, inherit.aes = FALSE ) +
  ggtitle("Title is top 10")+
  theme(plot.title = element_text(hjust = 0.5, unit(c(2.1, 0, -1, 0), "cm") ))