通過R腳本實現(xiàn)庫存分析的兩個內容榴芳,一是庫存結余量隨時間變化的折線圖和月同柱狀圖,二是各個門店庫存結余隨時間變化的熱力圖辜荠。
R腳本繪制折線圖和條形圖
library(tidyverse)
library(readxl)
library(patchwork)
df1 <- read_xlsx("庫存分析數(shù)據(jù).xlsx")
head(df1)
df1$`出售年月(年月日)` = as.Date(df1$`出售年月(年月日)`,"%Y-%m-%d")
df1$`品類上新時間(年月日)` = as.Date(df1$`品類上新時間(年月日)`,"%Y-%m-%d")
#is.na(df1)
df_n <- df1 %>%
mutate(month = cut(`出售年月(年月日)`,breaks = "month")) %>%
group_by(month) %>%
summarise_if(is.numeric,~sum(.)) %>%
mutate(月同比=c(0,(本期結存量[-1] - 本期結存量[-15])/本期結存量[-1])) %>%
select(month,本期結存量,月同比)
p1<- df_n %>%
ggplot(aes(x=month,y=本期結存量,group = 1,color = "Orang"))+
geom_line() +
geom_point()+
theme_minimal()+
theme(legend.position = "none",axis.text.x = element_text(angle = 30, hjust=1))
p1
p2 <- df_n %>%
ggplot(aes(x=month,y=月同比))+
geom_bar(stat = "identity",fill = "#009E73")+
theme(axis.text.x = element_text(angle = 30, hjust=1))
p2
p1/p2
R腳本實現(xiàn)門店信息熱力圖
df_m <- df1 %>%
mutate(month = cut(`出售年月(年月日)`,breaks = "month")) %>%
group_by(month,門店) %>%
summarise_at(vars(本期結存量),~sum(.))
p1 <- df_m %>%
ggplot(aes(x = month, y= 門店,fill =本期結存量 )) +
geom_tile()+
theme(axis.text.x = element_text(angle = 30,hjust = 1)) +
scale_fill_viridis()+
labs(x = '',y='')+
theme(axis.text.x=element_blank()) +
guides(fill = FALSE)
p1
p2<- df_m%>%
filter( 門店!= "上海七莘路旗艦店") %>%
ggplot(aes(x = month, y= 門店,fill =本期結存量 )) +
geom_tile()+
theme(axis.text.x = element_text(angle = 30,hjust = 1)) +
scale_fill_viridis()
p2
p1 /p2
小結
R腳本實現(xiàn)以上兩個分析琼开,有幾個關鍵點。
- 時間信息的分組精算,
cut(
出售年月(年月日),breaks = "month")
瓢宦,完成按月分組,從而結合group_by函數(shù)和summarise 函數(shù)進行歸總 - 應用summarise 函數(shù)的各種變形灰羽,尤其是scope 應用驮履,使得數(shù)據(jù)歸總十分強大
- ggplot 繪圖在實現(xiàn)繪圖主題定制上腳本會比較多