【问题标题】:Avoid ggplot sorting the x-axis while plotting geom_bar()在绘制 geom_bar() 时避免 ggplot 对 x 轴进行排序
【发布时间】:2013-12-01 05:03:21
【问题描述】:

我想用 ggplot 绘制以下数据:

SC_LTSL_BM    16.8275
SC_STSL_BM    17.3914
proB_FrBC_FL   122.1580
preB_FrD_FL    18.5051
B_Fo_Sp    14.4693
B_GC_Sp    15.4986

我想做的是制作条形图并保持条形的顺序, (即以SC_LTSL_BM ...B_GC_Sp 开头)。但是默认行为 ggplot geom_bar 是对它们进行排序。我怎样才能避免这种情况?

  library(ggplot2)
  dat <- read.table("http://dpaste.com/1469904/plain/")
  pdf("~/Desktop/test.pdf")
  ggplot(dat,aes(x=V1,y=V2))+geom_bar()
  dev.off()

目前的图是这样的:

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    你需要告诉 ggplot 你已经有了一个有序的因子,所以它不会自动为你排序。

    dat <- read.table(text=
    "SC_LTSL_BM    16.8275
    SC_STSL_BM    17.3914
    proB_FrBC_FL   122.1580
    preB_FrD_FL    18.5051
    B_Fo_Sp    14.4693
    B_GC_Sp    15.4986", header = FALSE, stringsAsFactors = FALSE)
    
    # make V1 an ordered factor
    dat$V1 <- factor(dat$V1, levels = dat$V1)
    
    # plot
    library(ggplot2)
    ggplot(dat,aes(x=V1,y=V2))+geom_bar(stat="identity")
    

    【讨论】:

    • 而且,从技术上讲,它确实为您订购。默认值是按字母顺序排列的——这几乎不是您想要的,但很难想象一个更合理的默认值。
    • @Gregor 我不可能知道它是按字母顺序排列的,直到你提到它。谢谢
    【解决方案2】:

    这里是一种不修改原始数据,而是使用scale_x_discrete的方法。来自?scale_x_discrete,“使用限制来调整显示的级别(以及以什么顺序)”。例如:

    dat <- read.table(text=
                    "SC_LTSL_BM    16.8275
                  SC_STSL_BM    17.3914
                  proB_FrBC_FL   122.1580
                  preB_FrD_FL    18.5051
                  B_Fo_Sp    14.4693
                  B_GC_Sp    15.4986", header = FALSE, stringsAsFactors = FALSE)
    # plot
    library(ggplot2)
    ggplot(dat,aes(x=V1,y=V2))+
      geom_bar(stat="identity")+
      scale_x_discrete(limits=dat$V1)
    

    【讨论】:

    • 我认为这是更好的答案,因为它与堆积条形图兼容,在列中重复相同的 ID,因此与转换为可排序因子不兼容。
    【解决方案3】:

    dplyr 可让您轻松创建 row 列,您可以在 ggplot 中重新排序。

    library(dplyr)
    dat <- read.table("...") %>% mutate(row = row_number())
    ggplot(df,aes(x=reorder(V1,row),y=V2))+geom_bar()
    

    【讨论】:

      【解决方案4】:

      您也可以按照here 的描述重新排序相应的因子

      x$name <- factor(x$name, levels = x$name[order(x$val)])
      

      【讨论】:

        【解决方案5】:

        如果要避免更改原始数据,则可以使用 fct_inorder from forcatstidyverse 的一部分)来保持数据沿 x 轴的原始顺序(而不是被更改)按字母顺序)。

        library(tidyverse)
        
        ggplot(dat, aes(x = fct_inorder(V1), y = V2)) +
          geom_bar(stat = "identity")
        

        输出

        forcats 的另一个选项是使用fct_relevel 手动指定顺序。

        ggplot(dat, aes(
          x = fct_relevel(
            V1,
            "SC_LTSL_BM",
            "SC_STSL_BM",
            "proB_FrBC_FL",
            "preB_FrD_FL",
            "B_Fo_Sp",
            "B_GC_Sp"
          ),
          y = V2
        )) +
          geom_bar(stat = "identity") +
          xlab("Category")
        

        数据

        dat <- structure(list(
          V1 = c(
            "SC_LTSL_BM",
            "SC_STSL_BM",
            "proB_FrBC_FL",
            "preB_FrD_FL",
            "B_Fo_Sp",
            "B_GC_Sp"
          ),
          V2 = c(16.8275, 17.3914,
                 122.158, 18.5051, 14.4693, 15.4986)
        ),
        class = "data.frame",
        row.names = c(NA, -6L))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-07-01
          • 2011-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-19
          相关资源
          最近更新 更多