最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Adding a X axis title to faceted ggballoonplot - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question edited Mar 6 at 17:17 stefan 127k6 gold badges38 silver badges76 bronze badges Recognized by R Language Collective asked Mar 6 at 16:58 DoctorSpruceDoctorSpruce 1591 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布评论

评论列表(0)

  1. 暂无评论