I followed the code on this page to create balloon plots with ggballoonplot() using facet.by. I wanted to add an x axis title, so I followed what was recommended in the question asked here to which described how to add an axis label to a balloon plot.
library(ggplot2)
df <- as.data.frame(HairEyeColor)
p<-ggballoonplot(df, x = "Hair", y = "Eye", size = "Freq",
fill = "Freq", facet.by = "Sex",
ggtheme = theme_bw()) +
scale_fill_viridis_c(option = "C")+
xlab("Group")
Which produces this plot, missing the xlab that I thought I had specified.
I also tried to assign an xlab as follows, but it also didn't work as planned (produces the same plot as shown above).
p2<-ggballoonplot(df, x = "Hair", y = "Eye", size = "Freq",
fill = "Freq", facet.by = "Sex",
ggtheme = theme_bw()) +
scale_fill_viridis_c(option = "C")+
labs(x = "Group")
Any ideas? As per the SO post I linked to, someone suggested it might be a print/rendering settings issue, but they didn't offer any suggestions on how to pursue that possibility and resolve the issue. Thanks!
The issue is that under the hood ggballoonplot
removes the axis title via theme()
, i.e. axis.title.x = element_blank()
. Hence, adding a title via xlab()
or labs()
has no effect. Instead you have to override the theme()
element, too, using axis.title.x = element_text()
:
library(ggplot2)
library(ggpubr)
df <- as.data.frame(HairEyeColor)
ggballoonplot(df,
x = "Hair", y = "Eye", size = "Freq",
fill = "Freq", facet.by = "Sex",
ggtheme = theme_bw()
) +
scale_fill_viridis_c(option = "C") +
xlab("Group") +
theme(axis.title.x = element_text())