代码之家  ›  专栏  ›  技术社区  ›  delcast

为什么在使用ggplot进行切面时,我的barplot没有正确地重新排列?

  •  0
  • delcast  · 技术社区  · 6 年前

    enter image description here

     na.omit(insect_tally_native_ranges)%>%
      group_by(native_ranges)%>%
      dplyr::summarise(freq=sum(n))%>%
      ggplot(aes(x=reorder(native_ranges,freq),y=freq))+
      geom_col(color="#CD4F39",fill="#CD4F39",alpha=0.8)+
      coord_flip()+
      labs(x="Native ranges",
           y="Number of invasive insect arrivals",
           title="Species by native ranges")+
      theme_minimal()
    

    现在我想做同样的事情,但是用一个叫做 Period

    ggplot(native_freq_period,
             aes(y=reorder(native_ranges,freq),x=freq))+
      geom_barh(stat= "identity",
                color="#CD4F39",
                fill="#CD4F39",
                alpha=0.8)+
      labs(x="Native ranges",
           y="Number of invasive insect arrivals",
           title="Species by native ranges")+
      theme_minimal()+
      facet_wrap(~Period)
    

    但情节是这样的:

    enter image description here

    这很烦人,因为它与上面的代码和变量的级别相同 native_ranges reorder 零件正在重新排序,但不是由 freq ! 我不明白。

    数据如下:

    structure(list(native_ranges = structure(c(6L, 10L, 11L, 7L, 
    3L, 5L, 1L, 1L, 8L, 6L, 3L, 5L, 2L, 4L, 5L, 7L, 7L, 7L, 8L, 9L, 
    11L), .Label = c("Afrotropic", "Afrotropic/Neotropic", "Australasia", 
    "Australasia/Neotropic", "Indomalaya", "Nearctic", "Neotropic", 
    "Neotropic/Nearctic", "Neotropic/Nearctic/Australasia", "Palearctic", 
    "Palearctic/Indomalaya"), class = "factor"), Period = structure(c(4L, 
    4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 3L, 3L, 4L, 4L, 2L, 1L, 2L, 
    3L, 2L, 4L, 3L), .Label = c("1896-1925", "1926-1955", "1956-1985", 
    "1986-2018"), class = "factor"), freq = c(21L, 13L, 12L, 11L, 
    10L, 10L, 4L, 4L, 4L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L)), row.names = c(NA, -21L), class = c("grouped_df", "tbl_df", 
    "tbl", "data.frame"), vars = "native_ranges", drop = TRUE, indices = list(
        6:7, 12L, c(4L, 10L), 13L, c(5L, 11L, 14L), c(0L, 9L), c(3L, 
        15L, 16L, 17L), c(8L, 18L), 19L, 1L, c(2L, 20L)), group_sizes = c(2L, 
    1L, 2L, 1L, 3L, 2L, 4L, 2L, 1L, 1L, 2L), biggest_group_size = 4L, labels = structure(list(
        native_ranges = structure(1:11, .Label = c("Afrotropic", 
        "Afrotropic/Neotropic", "Australasia", "Australasia/Neotropic", 
        "Indomalaya", "Nearctic", "Neotropic", "Neotropic/Nearctic", 
        "Neotropic/Nearctic/Australasia", "Palearctic", "Palearctic/Indomalaya"
        ), class = "factor")), row.names = c(NA, -11L), class = "data.frame", vars = "native_ranges", drop = TRUE))
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Ben G    6 年前

    summary_data <- data %>%
     ungroup() %>%
     group_by(native_ranges) %>%
     summarize(total = sum(freq))
    
    data <- data %>%
     left_join(summary_data)
    
    ggplot(data, aes(y = reorder(native_ranges, total),x = freq)) +
     geom_barh(stat= "identity",
               color="#CD4F39",
               fill="#CD4F39",
               alpha=0.8) +
     labs(x="Native ranges",
          y="Number of invasive insect arrivals",
          title="Species by native ranges") +
     theme_minimal()+
     facet_wrap(~Period)
    

    enter image description here

        2
  •  2
  •   rm167    6 年前

    在绘图之前,必须先排列变量的顺序。由于你没有提供任何可复制的数据,我使用以下数据

    drugs <- data.frame(drug = c("a", "b", "c"), effect = c(4.2, 9.7, 6.1))
    ggplot(drugs, aes(drug, effect)) +
      geom_col()
    

    enter image description here

    现在更改变量的使用顺序 factor

    drugs$drug <- factor(drugs$drug,levels = c("b","a","c")) #This is the order I want 
    ggplot(drugs, aes(drug, effect)) +
      geom_col()
    

    enter image description here

    在这里我提供了 levels 因素 手动。您可以手动提供它们,也可以先分别对变量的顺序进行排序并提供。见下文,

    drugs$drug <- factor(drugs$drug,levels = drugs[order(drugs$effect),]$drug)
    ggplot(drugs, aes(drug, effect)) +
      geom_col()
    

    enter image description here

    facet_wrap 也。

    推荐文章