如何根据两个变量重新排序 facet_wrap
How to reorder facet_wrap based on two variables
我需要根据两个变量(points
和 type
)绘制条形图并进行填充。
下面是一个最小的例子,我想看看按警戒点排名和后卫或前锋得分。
我尝试了 ~reorder(names, -c(type, points))
但它不起作用。
name <- c("James Harden","James Harden","Lebron James","Lebron James","Lebron James","Kawhi Leonerd","Kawhi Leonerd","Klay Thompson","Steph Curry","Kevin Durant","Kevin Durant","Chris Paul","Chris Paul")
team <- c("HOU","OKC","LAL","MIA","CLE","SAS","TOR","GSW","GSW","GSW","OKC","HOU","LAC")
points <- c(2000,12000,2000,10000,20000,7000,2000,14000,20000,6000,18000,4000,14000)
type <- c("G","G","F","G","F","G","G","G","G","F","F","G","G")
nba <- data.frame(name,team,points,type)
nba <- nba %>% arrange(desc(type))
ggplot(nba, aes(x = type, y = points, fill = team)) +
geom_bar(stat = 'identity', position = 'stack', color = 'black') +
facet_wrap(~reorder(name,-points), ncol = 1, strip.position = "top") +
coord_flip() + theme_minimal() +
labs(x = "players", y = "points", title = "Rank by points as Guard")
如果按后卫得分排名,我希望 Steph Curry
排名第一,Chris Paul
第二,James Harden
和 Klay
并列第三,Lebron
排在第五,Kawhi
排在第六,KD
排在最后。
如果按得分排名后卫或前锋,我希望看到 Lebron
排名第一,KD
第二,依此类推。
您可以通过添加辅助列来将其排序为守卫点数。往下看;
library(ggplot2)
library(dplyr)
nba %>%
mutate(guardpoints = points * (type=="G")) %>%
ggplot(aes(x = type, y = points, fill = team)) +
geom_bar(stat = 'identity', position = 'stack', color = 'black') +
facet_wrap(~reorder(name, -guardpoints, sum), ncol = 1, strip.position = "top") +
coord_flip() + theme_minimal() +
labs(x = "players", y = "points", title = "Rank by points as Guard")
nba %>%
ggplot(aes(x = type, y = points, fill = team)) +
geom_bar(stat = 'identity', position = 'stack', color = 'black') +
facet_wrap(~reorder(name, -points, sum), ncol = 1, strip.position = "top") +
coord_flip() + theme_minimal() +
labs(x = "players", y = "points", title = "Rank by points")
由 reprex package (v0.3.0)
于 2019-06-04 创建
我需要根据两个变量(points
和 type
)绘制条形图并进行填充。
下面是一个最小的例子,我想看看按警戒点排名和后卫或前锋得分。
我尝试了 ~reorder(names, -c(type, points))
但它不起作用。
name <- c("James Harden","James Harden","Lebron James","Lebron James","Lebron James","Kawhi Leonerd","Kawhi Leonerd","Klay Thompson","Steph Curry","Kevin Durant","Kevin Durant","Chris Paul","Chris Paul")
team <- c("HOU","OKC","LAL","MIA","CLE","SAS","TOR","GSW","GSW","GSW","OKC","HOU","LAC")
points <- c(2000,12000,2000,10000,20000,7000,2000,14000,20000,6000,18000,4000,14000)
type <- c("G","G","F","G","F","G","G","G","G","F","F","G","G")
nba <- data.frame(name,team,points,type)
nba <- nba %>% arrange(desc(type))
ggplot(nba, aes(x = type, y = points, fill = team)) +
geom_bar(stat = 'identity', position = 'stack', color = 'black') +
facet_wrap(~reorder(name,-points), ncol = 1, strip.position = "top") +
coord_flip() + theme_minimal() +
labs(x = "players", y = "points", title = "Rank by points as Guard")
如果按后卫得分排名,我希望 Steph Curry
排名第一,Chris Paul
第二,James Harden
和 Klay
并列第三,Lebron
排在第五,Kawhi
排在第六,KD
排在最后。
如果按得分排名后卫或前锋,我希望看到 Lebron
排名第一,KD
第二,依此类推。
您可以通过添加辅助列来将其排序为守卫点数。往下看;
library(ggplot2)
library(dplyr)
nba %>%
mutate(guardpoints = points * (type=="G")) %>%
ggplot(aes(x = type, y = points, fill = team)) +
geom_bar(stat = 'identity', position = 'stack', color = 'black') +
facet_wrap(~reorder(name, -guardpoints, sum), ncol = 1, strip.position = "top") +
coord_flip() + theme_minimal() +
labs(x = "players", y = "points", title = "Rank by points as Guard")
nba %>%
ggplot(aes(x = type, y = points, fill = team)) +
geom_bar(stat = 'identity', position = 'stack', color = 'black') +
facet_wrap(~reorder(name, -points, sum), ncol = 1, strip.position = "top") +
coord_flip() + theme_minimal() +
labs(x = "players", y = "points", title = "Rank by points")
由 reprex package (v0.3.0)
于 2019-06-04 创建