我有一個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
但我仍然沒有得到我想要的。
uj5u.com熱心網友回復:
利用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()
uj5u.com熱心網友回復:
簡單地排列資料集對類別的順序沒有影響。相反,您必須將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")
uj5u.com熱心網友回復:
base R
使用factor
您定義levels=c(sort(unique()))
字母順序的選項:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/498302.html