rggplot2bar-chartggtitle

Plot title not working in Circular Barplot


I'm trying to add title to my circular barplot using ggtitle() but cannot find the title anywhere in the plot. I even tried adjusting the position but of no use. Could someone tell me the issue please? The sample input and code is as below:

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))

Solution

  • The issue is that you set the plot margins to a negative value of 2cm. Everything which is plotted in a distance or with a margin of less than 2cm from the plot margin, e.g. the title, will be "cut off" or at least not displayed. To solve this adjust the margins of the title too:

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

    enter image description here