簡體   English   中英

ggplot2,排序y軸后代

[英]ggplot2, Ordering y axis descendent

我有一個像這樣的名為tb的數據框

   country station taxa scientific_name     var1
1      USA    GA01    A               a 23.42532
2      USA    GA02    A               b 23.10565
3      USA    GA03    A               c 23.88142
4      USA    GA04    B               d 23.18996
5      USA    GA05    B               e 23.32707
6      USA    GA06    B               f 23.78226
7      USA    GA07    C               g 23.11350
8      USA    GA08    C               h 23.67787
9      USA    GA09    F               i 23.69604
10     USA    GA10    F               l 23.68882
11     USA    GA11    F               m 23.90722
12     USA    GA12    F               n 23.23886
13     USA    GA13    F               o 23.53097
14     FRA    XC01    A               a 23.62910
15     FRA    XC02    A               b 23.08866
16     FRA    XC03    C               c 23.06928
17     FRA    XC04    C               d 23.72957
18     FRA    XC05    C               e 23.27950
19     FRA    XC06    D               f 23.37991
20     FRA    XC07    D               g 23.42608
21     FRA    XC08    D               h 23.54862
22     FRA    XC09    D               i 23.85336
23     FRA    XC10    D               l 23.93866
24     FRA    XC11    F               m 23.69664
25     FRA    XC12    F               n 23.72080
26     FRA    XC13    F               o 23.27925
27     FRA    XC14    F               p 23.38820
28     ITA    BV01    A               a 23.57040
29     ITA    BV02    B               b 23.10689
30     ITA    BV03    B               c 23.05586
31     ITA    BV04    C               d 23.98265
32     ITA    BV05    C               e 23.40625
33     ITA    BV06    C               f 23.93994
34     ITA    BV07    F               g 23.51695
35     ITA    BV08    F               h 23.48211
36     ITA    BV09    F               i 23.21569
37     ITA    BV10    F               l 23.08469

我正在使用 ggplot 繪制數據,但我沒有按照我的意願對 y 軸進行排序。

ggplot_df <- tb %>%
  group_by(country) %>%
  arrange(country,taxa,scientific_name)

ggplot_df$taxa_species_ordered <- paste(ggplot_df$taxa," - ",ggplot_df$scientific_name)

plot_species_zones <-
  ggplot(data = ggplot_df,
         aes(fill = country,
             x = taxa_species_ordered)) +
  geom_point(aes(y=var1),
             size = 3,
             shape = 21)+
  theme_bw() +
  guides(fill=FALSE, color=FALSE)+
  coord_flip()


show(plot_species_zones)

在這里,Y 軸上的字母按照自下而上的降序排列。 我想要一個自上而下的降序,其中 A 在頂部。

我嘗試將表格安排如下:

ggplot_df2 <- ggplot_df %>%
  arrange(taxa_species_ordered)

並根據ggplot_df2再次繪制,但我仍然不想要我想要的。

使用scale_x_discrete(limits=rev) +

library(tidyverse)
df %>%
  group_by(country) %>%
  arrange(country,taxa,scientific_name) %>% 
  ggplot(aes(fill = country,
             x = taxa)) +
  geom_point(aes(y=var1),
             size = 3,
             shape = 21)+
  scale_x_discrete(limits=rev) +
  theme_bw() +
  guides(fill=FALSE, color=FALSE)+
  coord_flip()

在此處輸入圖像描述

簡單地排列數據集對類別的順序沒有影響。 相反,您必須將您的taxa_species_ordered列轉換為一個因子並按照您想要的順序設置級別的順序。

為此,您可以先安排數據,然后通過forcats::fct_inorder設置順序。 然后,通過forcats::fct_rev反轉順序:

注意:此外,我切換到 x 和 y 的角色以擺脫coord_flip並使用例如fill = "none" instead of fill = FALSE`,因為后者已被棄用並且會導致警告。

library(dplyr)
library(forcats)
library(ggplot2)

ggplot_df <- tb

ggplot_df$taxa_species_ordered <- paste(ggplot_df$taxa, " - ", ggplot_df$scientific_name)

ggplot_df <- ggplot_df %>%
  arrange(taxa, scientific_name) |> 
  mutate(taxa_species_ordered = fct_rev(fct_inorder(taxa_species_ordered)))

ggplot(
  data = ggplot_df,
  aes(
    fill = country,
    y = taxa_species_ordered
  )
) +
  geom_point(aes(x = var1),
    size = 3,
    shape = 21
  ) +
  theme_bw() +
  guides(fill = "none", color = "none")

在此處輸入圖像描述

base R選項使用您定義字母順序的levels=c(sort(unique()))factor

library(ggplot2)
library(dplyr)
ggplot_df <- tb %>%
  group_by(country) %>%
  arrange(country,taxa,scientific_name)

ggplot_df$taxa_species_ordered <- paste(ggplot_df$taxa," - ",ggplot_df$scientific_name)

ggplot_df$taxa_species_ordered <- factor(ggplot_df$taxa_species_ordered, levels = c(sort(unique(ggplot_df$taxa_species_ordered), decreasing = TRUE)))

plot_species_zones <-
  ggplot(data = ggplot_df,
         aes(fill = country,
             x = taxa_species_ordered)) +
  geom_point(aes(y=var1),
             size = 3,
             shape = 21)+
  theme_bw() +
  guides(fill=FALSE, color=FALSE)+
  coord_flip()
#> Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
#> "none")` instead.


show(plot_species_zones)

reprex 包於 2022-07-02 創建 (v2.0.1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM