Search code examples
rggplot2groupingggh4x

How to change the label angle in ggh4x package after using scale_x_discrete


Here is my sample data and code 1 ,2 and not perfect 3

dat <- data.frame(value=runif(26)*10,
                  grouping=c(rep("Group 1",10),
                             rep("Group 2",10),
                             rep("Group 3",6)),
                  letters=LETTERS[1:26])
head(dat)

##
dat2 <- data.frame(value=runif(26)*10,
                   Disease=c(rep("disease1",13),rep("disease2",13)),
                   grouping=c(rep("Group 1",10),
                              rep("Group 2",10),
                              rep("Group 3",6)),
                   letters=LETTERS[1:26])

###  

dat3<-data.frame(value=runif(26)*10,
                 Disease=c(rep("disease1",13),rep("disease2",13)),
                 grouping=c(rep("Group 1Group 1Group 1",10),
                            rep("Group 2Group 1Group 1",10),
                            rep("Group 3Group 1Group 1",6)),
                 letters=paste0(LETTERS[1:26],"disease&Group"))

value grouping letters
1 9.7119440  Group 1       A
2 0.3838885  Group 1       B
3 7.7584126  Group 1       C
4 7.9419126  Group 1       D
5 3.7792050  Group 1       E
6 5.8977838  Group 1       F

##  ggh4x  1

ggplot(dat, aes(interaction(letters,grouping), value, fill=letters)) +
  geom_bar(position="dodge", stat="identity",width = 0.8,color="black") +
#  geom_text(position = position_dodge(width = 0.8), aes(x=grouping, y=0))+
  theme(legend.position = "none")+
  theme(axis.text.x=element_text(vjust=1,size=7))+
  labs(title = "Igfbp7", x = NULL, y = "FPKM_value")+
  scale_x_discrete(guide = "axis_nested")
##  rotate_x_text(angle = 45) 

#### 2
ggplot(dat2, aes(weave_factors(letters,grouping,Disease), value, fill=letters)) +
  geom_bar(position="dodge", stat="identity",width = 0.8,color="black") +
  #  geom_text(position = position_dodge(width = 0.8), aes(x=grouping, y=0))+
  theme(legend.position = "none")+
  theme(axis.text.x=element_text(vjust=1,size=10))+
  labs(title = "123", x = NULL, y = "value")+
  scale_x_discrete(guide = "axis_nested")+
  theme(plot.margin = unit(c(5, 8, 30, 7), "mm"))

##   3
ggplot(dat3, aes(weave_factors(letters,grouping,Disease), value, fill=letters)) +
  geom_bar(position="dodge", stat="identity",width = 0.8,color="black") +
  #  geom_text(position = position_dodge(width = 0.8), aes(x=grouping, y=0))+
  theme(legend.position = "none")+
  theme(axis.text.x=element_text(vjust=1,size=10))+
  labs(title = "123", x = NULL, y = "value")+
  scale_x_discrete(guide = "axis_nested")+
  theme(plot.margin = unit(c(5, 8, 30, 7), "mm"))+
  rotate_x_text(angle = 45)

I met some problems.

There are three grouping information that I need to add to the x axis.

And now I did it using the ggh4x package and the result like this: enter image description here

So I wanna know how to change the angle like the first label.


Solution

  • I realised I've documented quite poorly that there exists a ggh4x.axis.nesttext argument to the theme that you can use to control the looks of the text in nested guides. I should perhaps add it to the documentation's examples. Below you'll find an example on how to use it.

    library(ggplot2)
    library(ggh4x)
    
    dat3<-data.frame(value=runif(26)*10,
                     Disease=c(rep("disease1",13),rep("disease2",13)),
                     grouping=c(rep("Group 1Group 1Group 1",10),
                                rep("Group 2Group 1Group 1",10),
                                rep("Group 3Group 1Group 1",6)),
                     letters=paste0(LETTERS[1:26],"disease&Group"))
    
    ggplot(dat3, aes(weave_factors(letters,grouping,Disease), value, fill=letters)) +
      geom_bar(position="dodge", stat="identity",width = 0.8,color="black") +
      theme(legend.position = "none")+
      theme(axis.text.x=element_text(vjust=1,size=10))+
      labs(title = "123", x = NULL, y = "value")+
      scale_x_discrete(guide = "axis_nested")+
      theme(plot.margin = unit(c(5, 8, 30, 7), "mm"),
            axis.text.x = element_text(angle = 45, hjust = 1),
            ggh4x.axis.nesttext.x = element_text(angle = 45))
    

    Created on 2021-04-01 by the reprex package (v1.0.0)