我已經嘗試過該站點上類似問題的方法,但仍然無法解決這個問題。
我正在使用來自 kaggle 的 Titanic 資料集。我正在尋找的最終結果是,在每個 Pclass 因素中,我希望 age.class(條形)按 n 從低到高排序。我的嘗試如下。
library(tidyverse)
library(titanic)
df <- titanic::titanic_train
head(df)
# Start -------------------------------------------------------------------
df = df %>%
mutate(has.cabin = if_else(Cabin == '', 0, 1) %>% as.factor(),
Pclass = Pclass %>% as.factor(),
age.class = case_when(
Age < 5 ~ 'baby',
Age >5 & Age < 12 ~ 'Child',
Age > 12 & Age < 18 ~ 'Teen',
Age > 18 & Age < 25 ~ 'Young Adult',
Age > 25 & Age <35 ~ 'Mid Adult',
Age > 35 & Age < 60 ~ 'Adult',
Age > 60 ~ 'Elderly',
TRUE ~ 'Undefined'
)
)
plot.data = df %>% count(has.cabin, Pclass, age.class)
lvls <- unique(plot.data$Pclass[order(plot.data$age.class,-plot.data$n)])
plot.data$age.classv2 = factor(plot.data$age.class, levels=lvls)
plot.data %>%
ggplot(., aes(x = Pclass, y = n, fill = age.class))
geom_col(position = 'dodge')
facet_grid(~ has.cabin)
uj5u.com熱心網友回復:
我不知道有一種簡單的方法可以在方面內的一個因素內對因素進行排序。有功能
forcats::fct_reorder
重新排序因子tidytext::reorder_within
重新排序一個方面內的一個因素
我使用了第二個,Pclass
并制作了 2 個圖,一個用于has.cabin == 0
,一個用于has.cabin == 1
,然后將它們縫合在一起。
引數需要一個單獨的變數,fill
因為在內部會reorder_within
生成多個附加了構面名稱的變數。如果您不使用額外變數,那么您會看到這些名稱,請參閱Julia Silge 博客中的評論。
library(titanic)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidytext)
library(ggplot2)
library(patchwork)
df <- titanic::titanic_train
df <- df %>%
mutate(has.cabin = if_else(Cabin == '', 0, 1) %>% as.factor(),
Pclass = as.factor(Pclass),
age.class = case_when(
Age < 5 ~ 'baby',
Age >5 & Age < 12 ~ 'Child',
Age > 12 & Age < 18 ~ 'Teen',
Age > 18 & Age < 25 ~ 'Young Adult',
Age > 25 & Age <35 ~ 'Mid Adult',
Age > 35 & Age < 60 ~ 'Adult',
Age > 60 ~ 'Elderly',
TRUE ~ 'Undefined'
)
)
p1 <- df %>%
count(has.cabin, Pclass, age.class) %>%
filter(has.cabin == "0") %>%
mutate(age.class.plot = reorder_within(age.class, n, Pclass),
Pclass = paste0("Plcass ", Pclass)) %>%
ggplot(aes(x = age.class.plot, y = n, fill = age.class))
geom_col(position = 'dodge')
scale_x_reordered()
facet_grid(~ Pclass, scales = "free_x")
theme(
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank()
)
labs(title = "has.cabin 0")
coord_cartesian(ylim = c(0, 180))
p2 <- df %>%
count(has.cabin, Pclass, age.class) %>%
filter(has.cabin == "1") %>%
mutate(age.class.plot = reorder_within(age.class, n, Pclass),
Pclass = paste0("Plcass ", Pclass)) %>%
ggplot(aes(x = age.class.plot, y = n, fill = age.class))
geom_col(position = 'dodge')
scale_x_reordered()
facet_grid(~ Pclass, scales = "free_x")
theme(
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank()
)
labs(title = "has.cabin 1")
coord_cartesian(ylim = c(0, 180))
p1 p2 plot_layout(guides = "collect")
由reprex 包于 2022-06-30 創建(v1.0.0)
我認為為了在一個方面內的一個因素內重新排序一個因素,需要適應reorder_within
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/498310.html
標籤:r ggplot2 dplyr tidyverse 数据可视化