如何在分组的Barplot中排名类别?
这是按年份和模型按销售数据的数据。现在,我要按以下方式来对这些模型进行排名/订购:
模型必须根据其每年的销售的销售量从上到下进行排序。例如,E在2015年的销售量最大,因此必须在顶部,而在2017年H中必须位于顶部。
此外,我需要保持模型 j 始终在底部无论其份额如何。
library(dplyr)
library(ggplot2)
df <- data.frame (model = c("A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J"),
Year = c(2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018),
sales = c(450,678,456,344,984,456,234,244,655,789,234,567,234,567,232,900,1005,1900,450,345,567,235,456,345,144,333,555,777,111,444,222,223,445,776,331,788,980,1003,456,434))
df %>%
group_by(Year) %>%
mutate(Share = sales / sum(sales)) %>%
mutate_at(vars(Share), funs(round(., 4))) %>%
ggplot(aes(fill=model, y=Share, x=Year))+
scale_x_continuous(breaks=seq(min(df$Year),max(df$Year),2))+
geom_col(position="fill", width = 1, color = "white") +
geom_text(aes(label = scales::percent(Share, accuracy = 0.1)),
position = position_fill(vjust = 0.50),
color = "black",size = 2) +
scale_y_continuous(labels = scales::percent)
Here is data of sales by year and model. Now I wan to rank/order these models by following:
Models has to be ordered from top to down according to their sales in each year. For example, E had the biggest sales in 2015, so it must the on the TOP, while in 2017 H must be on the TOP.
Moreover, I need to keep model J always on the bottom regardless its share.
library(dplyr)
library(ggplot2)
df <- data.frame (model = c("A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J"),
Year = c(2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018),
sales = c(450,678,456,344,984,456,234,244,655,789,234,567,234,567,232,900,1005,1900,450,345,567,235,456,345,144,333,555,777,111,444,222,223,445,776,331,788,980,1003,456,434))
df %>%
group_by(Year) %>%
mutate(Share = sales / sum(sales)) %>%
mutate_at(vars(Share), funs(round(., 4))) %>%
ggplot(aes(fill=model, y=Share, x=Year))+
scale_x_continuous(breaks=seq(min(df$Year),max(df$Year),2))+
geom_col(position="fill", width = 1, color = "white") +
geom_text(aes(label = scales::percent(Share, accuracy = 0.1)),
position = position_fill(vjust = 0.50),
color = "black",size = 2) +
scale_y_continuous(labels = scales::percent)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要每年添加堆叠的列,以便能够在每列上使用单个分类。利用一些
didyverse
帮助者可以尝试以下操作:nb 亲自,我发现很难读取该图,因为您始终需要参考传说以确定显示哪个模型。
You need to add the stacked columns per year to be able to use individual sorting on each column. Making use of some
tidyverse
helpers you can try the following:N.B. Personally, I find the plot hard to read as you always need to refer to the legend to determine which model is shown.