使用scale_x_discrete后如何更改ggh4x包中的标签角度

问题描述 投票:0回答:1

这是我的示例数据和代码 1 ,2 且不完美 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)

我遇到了一些问题。

我需要将三个分组信息添加到x轴。

现在我使用 ggh4x 包完成了它,结果如下:

所以我想知道如何像第一个标签一样改变角度。

r ggplot2 grouping ggh4x
1个回答
2
投票

我意识到我的记录很差,主题存在一个

ggh4x.axis.nesttext
参数,您可以使用它来控制嵌套指南中文本的外观。我也许应该将其添加到文档的示例中。您将在下面找到有关如何使用它的示例。

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

reprex 包于 2021 年 4 月 1 日创建(v1.0.0)

© www.soinside.com 2019 - 2024. All rights reserved.