ls()
rm(list=ls())
ls()
shop_data<-read.csv("C:/Users/yjk9/Desktop/final_data.csv")
head(shop_data)
dim(shop_data)
#자료:37372개
library(ggplot2)
library(dplyr)
#shop_data<-shop_data%>%filter(total_sales!=0)
#dim(shop_data)
#total_sales가 0인걸 제외하니 자료 35379개
#####################################################################
#total_sales 총 판매액이 0인 데이터는 아예 예측시 제외해야 하는지 메일로 물어봄
#기온
ggplot(data=shop_data,aes(x=temp,y=total_sales))+geom_point()
ggplot(data=shop_data,aes(x=temp,y=Sales_quantity))+geom_point()
#계절별 판매량을 본다
max(shop_datatotal_sales)
min(shop_datatemp)
max(shop_data$week)
#53주까지 있음
#봄 3~5월/10~22주차
#여름 6~8월/23~35주차
#가을 9~11월/36~48주차
#겨울 12~2월/1~9주차+49~53주차
shop_dataweek>=10&shop_dataweek>=23&shop_dataweek>=36&shop_data$week<=48,"fall","winter")))
a<-shop_data%>%filter(season=="spring")%>%summarise(mean(Sales_quantity))
b<-shop_data%>%filter(season=="summer")%>%summarise(mean(Sales_quantity))
c<-shop_data%>%filter(season=="fall")%>%summarise(mean(Sales_quantity))
d<-shop_data%>%filter(season=="winter")%>%summarise(mean(Sales_quantity))
season<-c("spring","summer","fall","winter")
quantity<-c(a,b,c,d)
season_quantity<-data.frame(season,quantity)
ggplot(data=season_quantity)+geom_col(aes(x=season,y=quantity))+ggtitle("계절별 평균 판매량")
#강수량
min(shop_dataprecipitation)
#일단 y측 갯수로 핵가지고 산점도
ggplot(data=shop_data)+geom_point(aes(x=precipitation,y=total_sales/unit_price))
#0~5사이에 자료들이 몰려 있으므로
#0~5를 한그룹으로 보고 5~21을 또다른 한 그룹으로 보아 서로의 평균 매출액 비교
rain_small<-shop_data%>% filter(precipitation>=0&precipitation<5)
rain_big<-shop_data%>%filter(precipitation>=5)
ggplot(data=rain_small)+geom_point(aes(x=precipitation,y=Sales_quantity))
ggplot(data=rain_big)+geom_point(aes(x=precipitation,y=Sales_quantity))
#small그룹과 big그룹 평균 비교
mean(rain_smallprecipitation)
shop_dataprecipitation>=0& shop_datarain_totalsales<-ifelse(shop_datatotal_sales),mean(rain_big$total_sales))
ggplot(data=shop_data)+geom_col(aes(x=rainvariable,y=rain_totalsales))
#다시 나눠봄 4개그룹으로
shop_dataprecipitation>=0& shop_dataprecipitation>=3&shop_dataprecipitation>=5&shop_datarain_quantity<-ifelse(shop_datarainvariable==2,mean2,ifelse(shop_data$rainvariable==3,mean3,mean4)))
rainframe<-data.frame(rain=c(1,2,3,4),
quantity=c(297.3657,275.5667,280.8862,479.7869))
rainframe
ggplot(data=rainframe)+geom_col(aes(x=rain,y=quantity))+ggtitle("강수량 그룹별 평균 판매량")
#강수량이 높은 4그룹에서 판매량이 높아지는 걸 확인할 수 있음
#최저기온,최고기온,평균기온은 하루매출총액으로 따져야 할듯함
#일별 총판매량 구하기/일별 평균 판매량
#tempdata$day_quantity
#최저기온
#str(shop_data)
#ggplot(data=shop_data)+geom_point(aes(y=low_temp,x=total_sales/unit_price))
#최고기온
#ggplot(data=shop_data)+geom_point(aes(x=high_temp,y=total_sales/unit_price))
#평균기온
#미세먼지
#미세없는날0, 주의보1, 경보2로 데이터 변경
#3개의 그룹별 평균 판매량을 비교
str(shop_data)
b[is.na(b)]<-0
b<-ifelse(shop_datafine_dust=="경보",2,0))
b[is.na(b)]<-0
table(b)
head(b)
shop_data$dustvariable<-b
mean1<-shop_data %>% filter(b==0) %>% summarise(mean(Sales_quantity))
mean1
mean2<-shop_data %>% filter(b==1) %>% summarise(mean(Sales_quantity))
mean2
mean3<-shop_data %>% filter(b==2) %>% summarise(mean(Sales_quantity))
mean3
dust_quantity<-c(298.3561,283.3523,242.8)
dustframe<-data.frame(a=c(0,1,2),dust_quantity)
dustframe
ggplot(data=dustframe)+geom_col(aes(x=a,y=dust_quantity))+ggtitle("미세먼지 그룹별 평균 판매량")
#미세먼지에 따를 차이는 거의 없다고 보임
####이상 기상데이터 담당 김수현이었습니당
#- 상품군 편성 횟수는 count문제 생각해 봐야할 것 같아 질문을 넣지 않았습니다.
#- 휴일/비휴일의 경우 각자 해당하는 일수로 나눠야 할지(각 휴일/비휴일별 평균)?
##요일별##
week<- ggplot(data=shop_data, aes(x=day_week, y=total_sales))+
geom_col()+
scale_x_discrete(limits=c("월", "화","수","목","금","토","일"))+
ggtitle("요일별 판매실적")
week
#월요일 상품군에 따른 총 매출액
Monday<-shop_data %>% filter(day_week=="월")
ggplot(data=Monday)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("월요일 상품군 편성 횟수")
ggplot(data=Monday, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("월요일 상품군 총 매출액")
#화요일 상품군에 따른 총 매출액
Tuesday<-shop_data %>% filter(day_week=="화")
ggplot(data=Tuesday)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("화요일 상품군 편성 횟수")
ggplot(data=Tuesday, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("화요일 상품군 총 매출액")
#수요일 상품군에 따른 총 매출액
Wednesday<-shop_data %>% filter(day_week=="수")
ggplot(data=Wednesday)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("수요일 상품군 편성 횟수")
ggplot(data=Wednesday, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("수요일 상품군 총 매출액")
#목요일 상품군에 따른 총 매출액
Thursday<-shop_data %>% filter(day_week=="목")
ggplot(data=Thursday)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("목요일 상품군 편성 횟수")
ggplot(data=Thursday, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("목요일 상품군 총 매출액")
#금요일 상품군에 따른 총 매출액
Friday<-shop_data %>% filter(day_week=="금")
ggplot(data=Friday)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("금요일 상품군 편성 횟수")
ggplot(data=Friday, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("금요일 상품군 총 매출액")
#토요일 상품군에 따른 총 매출액
Saturday<-shop_data %>% filter(day_week=="토")
ggplot(data=Saturday)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("토요일 상품군 편성 횟수")
ggplot(data=Saturday, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("토요일 상품군 총 매출액")
#일요일 상품군에 따른 총 매출액
Sunday<-shop_data %>% filter(day_week=="일")
ggplot(data=Sunday)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("일요일 상품군 편성 횟수")
ggplot(data=Sunday, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("일요일 상품군 총 매출액")
##시간대별##
#주요 시간대의 상품군별 매출액 차지하는 정도?
#프라임 시간대의 상품군별 매출액은 어떻게 되는가?
Primetime<-shop_data %>% filter(time>=20 & time<=23)
ggplot(data=Primetime)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("프라임 시간대 상품군 별 편성 횟수")
ggplot(data=Primetime, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("프라임 시간대 상품군 별 총 매출액")
#비프라임
#비프라임 시간대의 상품군별 매출액은 어떻게 되는가?
```{r}
NPrimetime<-shop_data %>% filter(time>=13 & time<=16)
ggplot(data=NPrimetime)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("비프라임 시간대 상품군 별 편성 횟수")
ggplot(data=NPrimetime, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("비프라임 시간대 상품군 별 총 매출액")
#아침시간
#아침 시간대의 상품군별 매출액은 어떻게 되는가?
Morning<-shop_data %>% filter(time>=6 & time<=11)
ggplot(data=Morning)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("아침 시간대 상품군 별 편성 횟수")
ggplot(data=Morning, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("아침 시간대 상품군 별 총 매출액")
#오후시간
#오후 시간대의 상품군별 매출액은 어떻게 되는가?
Afternoon<-shop_data %>% filter(time>=17 & time<=18)
ggplot(data=Afternoon)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("오후 5-6 시간대 상품군 별 편성 횟수")
ggplot(data=Afternoon, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("오후 5-6 시간대 상품군 별 총 매출액")
#휴일/비휴일
#휴일/비휴일의 매출액 차이는 얼마나 나는가?
ggplot(data=shop_data, aes(x=holiday, y=total_sales, group=Product_group, color=Product_group))+
geom_col(position="stack")+
ggtitle("휴일/비휴일 총 매출액 비교")
#휴일의 제품군별 매출액 차이는 얼마나 나는가?
#휴일의 시간대별 매출액 추이?
holiday<-shop_data %>% filter(holiday==1)
ggplot(data=holiday)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("휴일 상품군 별 편성 횟수")
ggplot(data=holiday, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("휴일 상품군 별 총 매출액")
ggplot(data=holiday,aes(x=time, y=total_sales,group=Product_group,colour = Product_group))+
geom_col()+
ggtitle("휴일 시간대별 총 매출액")+
facet_wrap(~ Product_group)
ggplot(data=holiday,aes(x=time, y=total_sales,group=Product_group,colour = Product_group))+
geom_col()+
ggtitle("휴일 시간대별 총 매출액")
#비휴일의 제품군별 매출액 차이는 얼마나 나는가?
#비휴일의 시간대별 매출액 추이?
Nholiday<-shop_data%>% filter(holiday==0)
ggplot(data=Nholiday)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("비휴일 상품군 별 편성 횟수")
ggplot(data=Nholiday, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("비휴일 상품군 별 총 매출액")
ggplot(data=Nholiday,aes(x=time, y=total_sales,group=Product_group,colour = Product_group))+
geom_col()+
ggtitle("비휴일 시간대별 총 매출액")+
facet_wrap(~ Product_group)
ggplot(data=Nholiday,aes(x=time, y=total_sales,group=Product_group,colour = Product_group))+
geom_col()+
ggtitle("비휴일 시간대별 총 매출액")
time_holiday_counts <- shop_data %>%
group_by(time, Product_group, holiday) %>%
tally
ggplot(data = time_holiday_counts, aes(x = time, y = n, color = holiday, group = holiday)) +
geom_line() +
facet_wrap(~ Product_group)+
ggtitle("휴일/비휴일 상품군 별 편성 횟수 비교")
time_holiday_total <- shop_data %>%
group_by(time, Product_group, holiday) %>%
summarize(Rtotal_sales = sum(total_sales))
ggplot(data = time_holiday_total, aes(x = time, y = Rtotal_sales, color = holiday, group = holiday)) +
geom_line() +
facet_wrap(~ Product_group)+
ggtitle("휴일/비휴일 상품군 별 매출액 비교")
##주차별
byweek<-shop_data %>% group_by(week, Product_group) %>%
summarize(Rtotal_sales=sum(total_sales))
ggplot(data=byweek, aes(x=week, y=Rtotal_sales, group=Product_group, color=Product_group))+
geom_line()+
ggtitle("상품군 별 판매액 주차 추이")
#하루당 매출액 추이
#sales_per_day<-당일 매출액
sales_per_day<-shop_data %>% group_by(Date) %>%
summarize(total_sales_per_day=sum(total_sales))
sales_per_day$Date<-as.Date(substr(sales_per_day$Date,1,10))
sales_per_day<-sales_per_day[1:365,]
ggplot(data=sales_per_day, aes(x=Date, y=total_sales_per_day))+
geom_point()
#product_group count 편성 방법 /블록식
group_count1<-shop_data %>% group_by(Date,M_code,Product_group) %>%
summarize(count1=n())
group_count2<-group_count1[,1:3]
group_count_last<-group_count2 %>% group_by(Date,Product_group) %>%
summarize(count1=n())
#월별로 그래프를 그려보려고 한다.
#그런데 월별을 일단 character으로 바꿔줘보자
#월별로 총 판매 실적을 보고자 한다.
shop_data$month<-as.character(shop_data$month)
ggplot(data=shop_data, aes(x=month, y=total_sales))+
geom_col()+
scale_x_discrete(limits=c("1","2","3","4","5","6","7","8","9","10","11","12"))+
ggtitle("월별 총판매실적")
#12월의 총판매액이 가장 많고
#2월과 4월의 판매액이 가장 적다
#월별로 상품군에 따라 편성 횟수를 각각 보고자 한다.
month1<-shop_data %>% filter(month=="1")
ggplot(data=month1)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("1월달 상품군 편성 횟수")
ggplot(data=month1, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("1월달 상품군 총 매출액")
month2<-shop_data %>% filter(month=="2")
ggplot(data=month2)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("2월달 상품군 편성 횟수")
ggplot(data=month2, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("2월달 상품군 총 매출액")
month3<-shop_data %>% filter(month=="3")
ggplot(data=month3)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("3월달 상품군 편성 횟수")
ggplot(data=month3, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("3월달 상품군 총 매출액")
month4<-shop_data %>% filter(month=="4")
ggplot(data=month1)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("1월달 상품군 편성 횟수")
ggplot(data=month4, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("4월달 상품군 총 매출액")
month5<-shop_data %>% filter(month=="5")
ggplot(data=month5)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("5월달 상품군 편성 횟수")
ggplot(data=month5, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("5월달 상품군 총 매출액")
month6<-shop_data %>% filter(month=="6")
ggplot(data=month1)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("6월달 상품군 편성 횟수")
ggplot(data=month6, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("6월달 상품군 총 매출액")
month7<-shop_data %>% filter(month=="1")
ggplot(data=month1)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("7월달 상품군 편성 횟수")
ggplot(data=month7, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("7월달 상품군 총 매출액")
month8<-shop_data %>% filter(month=="8")
ggplot(data=month1)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("8월달 상품군 편성 횟수")
ggplot(data=month8, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("8월달 상품군 총 매출액")
month9<-shop_data %>% filter(month=="9")
ggplot(data=month9)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("1월달 상품군 편성 횟수")
ggplot(data=month9, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("9월달 상품군 총 매출액")
month10<-shop_data %>% filter(month=="10")
ggplot(data=month1)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("10월달 상품군 편성 횟수")
ggplot(data=month10, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("10월달 상품군 총 매출액")
month11<-shop_data %>% filter(month=="11")
ggplot(data=month1)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("11월달 상품군 편성 횟수")
ggplot(data=month11, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("11월달 상품군 총 매출액")
month12<-shop_data %>% filter(month=="12")
ggplot(data=month12)+
geom_bar(mapping=aes(x=Product_group))+
ggtitle("12월달 상품군 편성 횟수")
ggplot(data=month12, aes(x=Product_group, y=total_sales))+
geom_col(position="stack")+
ggtitle("12월달 상품군 총 매출액")
#한번에 그래프 그리기
#월별 총 매출액을 하나의 그래프로 합쳐 그렸다.
ggplot(data=shop_data, aes(x=month, y=total_sales, group=Product_group, color=Product_group))+
geom_col(position="stack")+
ggtitle("월별 총 매출액 비교")
#상품군별로 살펴본 월별 총 판매액 비교
ggplot(data = shop_data, aes(x = month, y = total_sales)) +
geom_line() +
facet_wrap(~ Product_group)+
ggtitle("상품군별로 살펴본 월별 총 판매액 비교")
#상품군별로 살펴본 월별 총 판매 횟수의 비교
ggplot(data = shop_data, aes(x = month)) +
geom_bar() +
facet_wrap(~ Product_group)+
ggtitle("상품군별로 살펴본 월별 총 판매 횟수의 비교")
#상품군 별 월별 총 판매액 주차 추이
bymonth<-shop_data %>% group_by(month, Product_group) %>%
summarize(Rtotal_sales=sum(total_sales))
ggplot(data=bymonth, aes(x=month, y=Rtotal_sales, group=Product_group, color=Product_group))+
geom_line()+
ggtitle("상품군 별 판매액 주차 추이")+scale_x_discrete(limits=c(1:12))
#환경 세팅
#데이터 불러오기
shop_data<- read.csv("C:/Users/yjk9/Desktop/final_data.csv",header=T,sep=",")
head(shop_data)
shop_data<- as.data.frame(shop_data)
library(ggplot2)
ggplot(data=shop_data)+geom_bar(mapping=aes(x=Product_code))+ggtitle("Histogram of Product_code")
library(tidyverse)
freq1<- shop_data %>% count(Product_code) #table과 같음
ggplot(data=freq1)+geom_histogram(aes(x=n),bins=50)+ggtitle("편성 횟수 분포")
pc_sales<- aggregate(shop_data$total_sales,by=list(shop_data$Product_code),FUN=sum)
pc_info<- data.frame(pc_sales,freq1[,2])
names(pc_info)<- c("Product_code","total_sales","freq")
mean_sales<- pc_info$total_sales/pc_info$freq #평균 매출액
pc_info<- data.frame(pc_info,mean_sales)
p<- subset(shop_data,select=c('Product_code','Product_name','Product_group','unit_price'))
product<- unique(merge(pc_info,p,by="Product_code"))
product<- data.frame(product, quantity=product$total_sales/product$unit_price)
head(product)
ggplot(data=product)+geom_col(aes(x=Product_code,y=total_sales))+ggtitle("상품 코드 당 전체 매출액")
product_order1<- product[order(-product$total_sales),]; head(product_order1)
ggplot(data=product)+geom_col(aes(x=Product_code,y=mean_sales))+ggtitle("상품 코드 당 한번 편성 했을 때의 평균 매출액 ")
product_order2<- product[order(-product$mean_sales),]; head(product_order2)
ggplot(data=product)+geom_col(aes(x=Product_code,y=quantity))+ggtitle("상품 코드 당 한번 편성 했을 때의 판매량")
product_order3<- product[order(-product$quantity),]; head(product_order3)
freq2<- shop_data %>% count(Product_group)
pg_sales<- aggregate(shop_data$total_sales,by=list(shop_data$Product_group),FUN=sum)
pg_mean_sales<- aggregate(shop_data$total_sales,by=list(shop_data$Product_group),FUN=mean)
pg_info<- data.frame(pg_sales,pg_mean_sales,freq2[,2])
pg_info<- pg_info[,-3]
names(pg_info)<- c("Product_group","total_sales","mean_sales","freq")
ggplot(data=pg_info)+geom_col(mapping=aes(x=Product_group,y=freq))+ggtitle("상품군별 편성 빈도")
ggplot(data=shop_data,aes(x=Product_group,y=total_sales))+geom_boxplot()+ggtitle("상품군별 매출액")
shop_data<-read.csv("C:/Users/yjk9/Desktop/final_data.csv")
head(shop_data)
attach(shop_data)
furniture <-shop_data %>% filter(Product_group=="가구")
furniture
ggplot(data=furniture, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("가구 min별 총 판매량")
clothes <-shop_data %>% filter(Product_group=="의류")
ggplot(data=clothes, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("의류 min별 총 판매량")
underwear <-shop_data %>% filter(Product_group=="속옷")
ggplot(data=underwear, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("속옷 min별 총 판매량")
kitchen <-shop_data %>% filter(Product_group=="주방")
ggplot(data=kitchen, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("주방 min별 총 판매량")
agriculture <-shop_data %>% filter(Product_group=="농수축")
ggplot(data=agriculture, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("농수축 min별 총 판매량")
beauty <-shop_data %>% filter(Product_group=="이미용")
ggplot(data=beauty, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("이미용 min별 총 판매량")
electronics <-shop_data %>% filter(Product_group=="가전")
ggplot(data=electronics, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("가전 min별 총 판매량")
living <-shop_data %>% filter(Product_group=="생활용품")
ggplot(data=living, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("생활용품 min별 총 판매량")
health <-shop_data %>% filter(Product_group=="건강기능")
ggplot(data=health, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("건강기능 min별 총 판매량")
goods <-shop_data %>% filter(Product_group=="잡화")
ggplot(data=goods, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("잡화 min별 총 판매량")
bed <-shop_data %>% filter(Product_group=="침구")
ggplot(data=electronics, aes(x=min, y=Sales_quantity))+
geom_col(position="stack")+
ggtitle("침구 min별 총 판매량")