如何订购闪避条形图?
How to order a dodged bar chart?
我在按降序排列闪避条形图时遇到问题。该数据集包含城市和这些城市中特定项目的价格(例如出租车、饮料、晚餐等)——数据集可以在这里找到:https://data.world/makeovermonday/2018w48
代表:
City <- c("Mexico City", "Prague", "Moscow", "Mexico City","Prague", "Moscow")
Category <- c("Date Night", "Date Night","Date Night", "Party Night", "Party Night", "Party Night")
TotalCost <- c(84.82, 86.52, 20.35, 46.29, 19, 26.56)
CostNightPrepared <- data.frame(City,Category,TotalCost)
我修改了数据集,只显示 City
、Category
(晚上外出的类型)和 TotalCost
,这是每个城市每个类别的总价格:
CostNightPrepared <- CostNight %>%
group_by(City, Category) %>%
summarize(TotalCost = sum(Cost, na.rm = TRUE))%>%
arrange(Category, TotalCost)
可视化数据集:
ggplot(CostNightPrepared, aes(TotalCost, fct_rev(fct_reorder(City, TotalCost)), fill=Category)) +
geom_bar(stat="identity",position = position_dodge(width = 0.5))
如您所见,我尝试了 fct_rev
和 fct_reorder()
,但输出仍然是这样的:
如何按降序排列 'Party Night' 类别的闪避(重叠)条形图?
CostNightPrepared %>%
left_join(
CostNightPrepared %>%
filter(Category == "Party Night") %>%
arrange(-TotalCost) %>%
mutate(order = row_number()) %>%
select(City, order)
) %>%
ggplot(aes(TotalCost, forcats::fct_reorder(City, -order),
fill=Category)) +
geom_bar(stat="identity",position = position_dodge(width = 0.5))
有点破解,但解决此问题的一种方法是使用 fct_inorder
。这按照它们在数据框中首次出现的顺序对 City
中的值进行排序。将其与 arrange
结合使用以设置您需要的顺序:
library(tidyverse)
CostNightPrepared %>%
ungroup() %>%
mutate(Category = fct_relevel(Category, "Party night")) %>%
arrange(Category, TotalCost) %>%
mutate(City = fct_inorder(City)) %>%
ggplot(aes(x = TotalCost, y = City, fill = Category)) +
geom_bar(stat = "identity", position = "dodge")
'Party night'
类别已重新分级,以确保它在 arrange
步骤中排在顶部。如果需要,您可以将其重新升级。
先过滤'Party night'
的数据,汇总数据并提取城市名称,按Cost
.
的升序排列
CostNightPrepared %>%
filter(Category == 'Party night') %>%
group_by(City) %>%
summarise(aveg = mean(Cost)) %>%
arrange(aveg) %>%
pull(City) -> lvls
重新排列因子水平,汇总数据并作图。
CostNightPrepared %>%
mutate(City = factor(City, lvls)) %>%
group_by(City, Category) %>%
summarise(Cost = mean(Cost)) %>%
ggplot(aes(Cost, City, fill=Category)) +
geom_bar(stat="identity",position = position_dodge(width = 0.5))
我在按降序排列闪避条形图时遇到问题。该数据集包含城市和这些城市中特定项目的价格(例如出租车、饮料、晚餐等)——数据集可以在这里找到:https://data.world/makeovermonday/2018w48
代表:
City <- c("Mexico City", "Prague", "Moscow", "Mexico City","Prague", "Moscow")
Category <- c("Date Night", "Date Night","Date Night", "Party Night", "Party Night", "Party Night")
TotalCost <- c(84.82, 86.52, 20.35, 46.29, 19, 26.56)
CostNightPrepared <- data.frame(City,Category,TotalCost)
我修改了数据集,只显示 City
、Category
(晚上外出的类型)和 TotalCost
,这是每个城市每个类别的总价格:
CostNightPrepared <- CostNight %>%
group_by(City, Category) %>%
summarize(TotalCost = sum(Cost, na.rm = TRUE))%>%
arrange(Category, TotalCost)
可视化数据集:
ggplot(CostNightPrepared, aes(TotalCost, fct_rev(fct_reorder(City, TotalCost)), fill=Category)) +
geom_bar(stat="identity",position = position_dodge(width = 0.5))
如您所见,我尝试了 fct_rev
和 fct_reorder()
,但输出仍然是这样的:
如何按降序排列 'Party Night' 类别的闪避(重叠)条形图?
CostNightPrepared %>%
left_join(
CostNightPrepared %>%
filter(Category == "Party Night") %>%
arrange(-TotalCost) %>%
mutate(order = row_number()) %>%
select(City, order)
) %>%
ggplot(aes(TotalCost, forcats::fct_reorder(City, -order),
fill=Category)) +
geom_bar(stat="identity",position = position_dodge(width = 0.5))
有点破解,但解决此问题的一种方法是使用 fct_inorder
。这按照它们在数据框中首次出现的顺序对 City
中的值进行排序。将其与 arrange
结合使用以设置您需要的顺序:
library(tidyverse)
CostNightPrepared %>%
ungroup() %>%
mutate(Category = fct_relevel(Category, "Party night")) %>%
arrange(Category, TotalCost) %>%
mutate(City = fct_inorder(City)) %>%
ggplot(aes(x = TotalCost, y = City, fill = Category)) +
geom_bar(stat = "identity", position = "dodge")
'Party night'
类别已重新分级,以确保它在 arrange
步骤中排在顶部。如果需要,您可以将其重新升级。
先过滤'Party night'
的数据,汇总数据并提取城市名称,按Cost
.
CostNightPrepared %>%
filter(Category == 'Party night') %>%
group_by(City) %>%
summarise(aveg = mean(Cost)) %>%
arrange(aveg) %>%
pull(City) -> lvls
重新排列因子水平,汇总数据并作图。
CostNightPrepared %>%
mutate(City = factor(City, lvls)) %>%
group_by(City, Category) %>%
summarise(Cost = mean(Cost)) %>%
ggplot(aes(Cost, City, fill=Category)) +
geom_bar(stat="identity",position = position_dodge(width = 0.5))