【问题标题】:R ggplot multi-row x-axis labelsR ggplot多行x轴标签
【发布时间】:2020-01-21 00:11:54
【问题描述】:

这是我的数据(以 dput 格式):

    structure(list(Biological.Group = structure(1:15, .Label = c("A", 
"C", "E", "D", "F", "G", "W", "R", "T", "Y", "V", "B", "N", "M", 
"L"), class = "factor"), Group = c(NA, NA, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Relative.Normalized.Expression....Cq. = c(5L, 
7L, 3L, 5L, 6L, 8L, 9L, 1L, 4L, 5L, 7L, 4L, 2L, 7L, 8L), SE = c(0.171499719, 
0.089692493, 0.153777208, 0.188012958, 0.153776128, 0.192917199, 
0.224766056, 0.231338325, 0.121716906, 0.094763028, 0.09635363, 
0.069986333, 0.113681329, 0.094614957, 0.391182473)), row.names = c(NA, 
-15L), class = "data.frame")

这是我的图表的代码:

library(ggplot2)
colours <- c("A"="darkolivegreen4","C"="darkolivegreen2","E"="darkseagreen4","D"="darkseagreen3", "F"="gold3","G"="gold", "W"="goldenrod2","R"="yellow2","T"="mediumpurple","Y"="thistle1", "V"="turquoise","B"="pink3", "N"="plum4", "M"="tomato3", "L"="orange1")
df$Biological.Group <- as.character(df$Biological.Group)
df$Biological.Group <- factor(df$Biological.Group, 
levels=unique(df$Biological.Group))
plot = ggplot(df, aes(Biological.Group,Relative.Normalized.Expression....Cq.,fill=factor(Biological.Group)))+ scale_fill_manual(values = colours) + xlab("Condition") +ylab("Relative Normalized Expression (∆∆Cq)")+geom_bar(stat="identity")
plot + geom_errorbar(aes(ymin=Relative.Normalized.Expression....Cq.-SE,ymax=Relative.Normalized.Expression....Cq.+SE),width=0.5)+geom_boxplot() + theme(axis.text.x = element_text(angle = 60, hjust = 1)) + theme(legend.position="none") +ggtitle("Mrdf-1")+theme(plot.title = element_text(size=12, face="italic",hjust=0.5))
ggsave("dfGraph.png",height=5.64,width=5.64)

这是图表的样子:

我想将“组”列数据(第二列数据)作为 x 轴中的辅助标签,如下所示:

如果可能的话,我还想让组之间的空间更大(意思是“A”和“C”、“C”和“E”、“G”和“W”之间)

【问题讨论】:

    标签: r ggplot2 graph


    【解决方案1】:

    我对你的ggplot() 做了一些改动。

    • 无需使用factor()进行转换
    • 没有必要使用geom_boxplot()
    • geom_col()geom_bar(stat = "identity") 打字少
    • 不需要多个theme()
    • 我使用 labs() 表示 x、y 和标题标签

    Group的标注可以使用facet_grid()来实现。

    不确定“组之间的空间”是什么意思——从你的问题来看,它听起来更像是“条之间的空间”——这可以通过调整宽度来实现。

    我的代码:

    library(dplyr)
    
    df %>% 
      mutate(Group = ifelse(is.na(Group), "", Group)) %>% 
      ggplot(aes(Biological.Group, 
                 Relative.Normalized.Expression....Cq., 
                 fill = Biological.Group)) +
      geom_col(width = 0.7) + 
      geom_errorbar(aes(ymin = Relative.Normalized.Expression....Cq.-SE,
                        ymax = Relative.Normalized.Expression....Cq.+SE), width = 0.5) +
      scale_fill_manual(values = colours) + 
      labs(x = "Condition",
           y = "Relative Normalized Expression (∆∆Cq)",
           title = "Mrdf-1") +
      theme(axis.text.x = element_text(angle = 60, 
                                       hjust = 1),
            legend.position = "none",
            plot.title = element_text(size = 12, 
                                      face = "italic", 
                                      hjust = 0.5),
            strip.background.x = element_blank()) + 
      facet_grid(~Group, 
                 scales = "free_x", 
                 switch = "x", 
                 space = "free_x")
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-30
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 2018-05-19
      • 2020-10-06
      • 1970-01-01
      相关资源
      最近更新 更多