I am having a problem getting the title centered relative to the panel in the plot. If I output the plot as a PNG file and inspect the spacing to the left and right of the title with respect to the panel border, they don’t seem to be of equal lengths. I’m not sure how to correct this without manually adjustments
df <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(data = df,aes(x=x,y = y))+
geom_point(color = "yellow")+
theme(
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
axis.title = element_text(size = 7, face = "bold", colour = "white"),
plot.title = element_text(size = 45, face = "bold", colour = "white"),
legend.text = element_text(size = 13, colour = "white", face = "bold"),
legend.justification = "center",
legend.title = element_blank(),
legend.key = element_rect(fill = NA),
legend.background = element_rect(fill = "black"),
legend.position = "top",
legend.direction = "horizontal",
plot.margin = margin(1.5, 1, 1, 1, "cm"),
panel.grid.major.y = element_line(colour = "gray20"),
panel.grid.major = element_line(colour = "red", linetype = "solid"),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
axis.text = element_text(size = 17, face = "bold", colour = "white"),
legend.key.size = unit(0.5, "cm"),
legend.margin = margin(t = 0.5,b = 0.5,unit = "cm"),
legend.spacing.y = unit(0.01, 'cm'))+
labs(x ="EPIDEMIOLOGICAL WEEK OF 2024", y = NULL)+
theme(legend.box = "vertical",legend.box.just = "center")+
guides(fill = guide_legend(order = 1), color = guide_legend(order = 2))+
theme(axis.title.x = element_text(size = 15,hjust = 0.5,margin = margin(t = 25)))+
theme(rect = element_rect(fill = "black"))+
theme(panel.border = element_rect(colour = "white", fill = NA, size = 1))+
ggtitle("RSV IN SOUTH AFRICA")+
theme(plot.title.position = "panel")+
theme(
plot.title = element_text(hjust = 0.5))+
theme(axis.ticks.length=unit(0.1,"inch"))+
theme(axis.text.x = element_text(margin = margin(t = 5)))+
theme(axis.text.y = element_text(margin = margin(r = 5)))+
theme(axis.text.y.right = element_text(margin = margin(l = 5)))+
theme(axis.ticks = element_line(color = "white"))