我查看了許多答案(此處、此處、此處),但沒有一個能產生我想要的結果。我有一個行業資料集隨著時間的推移。我想要每個月按數量訂購的堆疊條。這意味著如果該月內的相對交易量發生變化,則每個月的堆疊條形圖都應具有不同的順序。
以下是截斷的資料示例:
test <- structure(list(Date = structure(c(18506, 18506, 18506, 18506,
18506, 18506, 18536, 18536, 18536, 18536, 18536, 18536, 18567,
18567, 18567, 18567, 18567, 18567), class = "Date"), Industry = c("Investment",
"Telecoms", "Mortgage & Loans", "Banking", "Insurance", "Credit Cards",
"Telecoms", "Investment", "Mortgage & Loans", "Banking", "Credit Cards",
"Insurance", "Investment", "Telecoms", "Mortgage & Loans", "Credit Cards",
"Insurance", "Banking"), volume = c(775349, 811294, 3144684,
4427814, 7062691, 9377254, 1210194, 1735033, 3539406, 6952688,
8858649, 9076391, 670934, 869452, 3542294, 5132132, 6953113,
6954535)), row.names = c(NA, -18L), groups = structure(list(Date = structure(c(18506,
18536, 18567), class = "Date"), .rows = structure(list(1:6, 7:12,
13:18), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = c(NA, -3L), class = c("tbl_df", "tbl",
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"))
這是到目前為止的圖表:
#A. Library
library(dplyr)
library(ggplot)
library(ggtext)
library(scales)
#B. Graph
graph <- test %>%
ggplot(aes(x=Date))
##1. Bar graph
geom_bar(aes(x=Date, y=volume, fill = Industry), stat="identity")
##2. Graph title and Axis labels
ggtitle(label = "**Volume**",
subtitle = "By Industry")
ylab("Volume (Millions)")
xlab("")
##3. Scales
scale_fill_manual(values=c("#e3120b", "#336666", "#FB9851", "#acc8d4",
"#dbcc98", "#36E2BD"))
scale_x_date(date_breaks = "month", labels = scales::label_date_short())
scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6,
accuracy = 1))
#4. Theme
guides(col = guide_legend(ncol = 2, nrow = 3))
theme_minimal()
theme(text = element_text(family = "Georgia"),
panel.border=element_blank(),
axis.line=element_line(),
plot.title = element_markdown(color="black", size=14, hjust = .5),
plot.subtitle = element_text(hjust = .5),
axis.title.x = element_text(size = 9, color = "grey30"),
axis.title.y = element_text(size = 9, color = "grey30"),
legend.box.background = element_rect(color="black", size=.5),
legend.title = element_blank(),
legend.text = element_text(size = 6),
legend.position = "bottom",
strip.background = element_rect(linetype="solid",),
panel.grid.minor.y = element_line(color = NA),
panel.grid.minor.x = element_line(color = NA),
plot.caption = ggtext::element_markdown(hjust = 1, size = 7,
color = "#7B7D7D"))
據我了解,ggplot按因子順序對堆疊的條形進行排序。我嘗試過,test %>% arrange(Date, volume)但后來被困在如何按月更改因子而不僅僅是因子的靜態順序。我可以使用單獨的因素為每個月創建一個單獨的條形圖,但是如果我想在圖表中添加多年,這會變得很麻煩。
任何幫助表示贊賞!
uj5u.com熱心網友回復:
我冒昧地將你的例子歸結為本質。根據評論,我認為沒有辦法單獨定義每個月的因子水平。但是您可以在函式中執行此操作,創建一個串列,并使用 ggplot 物件的串列字符。
這種方式是可擴展的,這意味著,無論您有多少個月,它都會保持相同的代碼...... :)
library(tidyverse)
library(lubridate)
test <-
test %>%
## it's probably not necessary to order the data and
## create the factor levels explicitly, but it gives more control
arrange(Date) %>%
mutate(year_mo = fct_inorder(paste(year(Date), month(Date), sep = "_")))
## split the new data by month and create different factor levels
ls_test <-
test %>%
split(., .$year_mo) %>%
map(function(x) {x$Industry <- fct_reorder(x$Industry, x$volume); x})
## make your geom_col list (geom_col is equivalent to geom_bar(stat= "identity")
ls_p_col <- map(ls_test, function(x){
geom_col(data = x, mapping = aes(x=year_mo, y=volume, fill = Industry))
})
# Voilà!
ggplot()
ls_p_col
scale_fill_brewer()
scale_x_discrete(limits = unique(test$year_mo)) # to force the correct order of your x

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/345865.html
