raw_data <- readRDS("car_data_aggregate.rds")
raw_data <- raw_data %>% filter(! (startsWith(brand_name, "ODD") | startsWith(brand_name, "TOPLAM")))
#ASTON MARTIN and ASTON MARTÄ°N are same brands
raw_data$brand_name <- str_replace(raw_data$brand_name,"ASTON MARTÄ°N","ASTON MARTIN")
Lets find the best selling 10 brands in Turkey. First we need to group the brands and summarize their sum of totals. After then we need to rank the data by using sum(total_total). Finally we will select 10 best selling brand and arrange them.
raw_data %>%
group_by(brand_name) %>%
summarize(sum = sum(total_total))%>%
mutate(rank = rank(-sum)) %>%
filter(rank <= 10) %>%
arrange(rank)
## # A tibble: 10 x 3
## brand_name sum rank
## <chr> <dbl> <dbl>
## 1 RENAULT 318500 1
## 2 FIAT 275900 2
## 3 FORD 271157 3
## 4 VOLKSWAGEN 262041 4
## 5 HYUNDAI 131666 5
## 6 DACIA 117978 6
## 7 OPEL 117122 7
## 8 TOYOTA 106950 8
## 9 PEUGEOT 98036 9
## 10 MERCEDES-BENZ 93944 10
Let’s examine bmw’s montly sales change. First we need to add new columns (date) for the chart. ggplot is used to draw the chart.
#First filter BMW data
bmw_data = raw_data %>% filter(brand_name == "BMW")
bmw_data %>%
mutate(date = as.Date(paste(year, month, 1, sep='-'))) %>%
ggplot(data = ., aes(x=date, y = total_total)) +
labs(y = "BMW Sales", x="Months", fill="Brands") +
geom_text(aes(label=total_total), vjust=-0.3, size=2.5) +
geom_bar(stat="identity", fill="steelblue")
Let’s examine montly sales change of BMW, Mercedes and Audi. First we need to add new columns (date) for the chart. ggplot is used to draw the chart.
#First filter these brands data
luxury_data = raw_data %>% filter(brand_name %in% c("BMW", "MERCEDES-BENZ", "AUDI"))
luxury_data %>%
mutate(date = as.Date(paste(year, month, 1, sep='-'))) %>%
ggplot(data = ., aes(x = date, y = total_total, color = brand_name)) +
labs(y = "The Sales of Each Brands", x="Months", fill="Brands") +
geom_line()
luxury_data %>%
group_by(brand_name, year) %>%
summarize(yearly_total = sum(total_total))%>%
ggplot(data=., aes(x=year, y=yearly_total, fill=brand_name)) +
geom_bar(stat="identity", position=position_dodge())+
scale_fill_brewer(palette="Paired")+
geom_text(aes(label=yearly_total), vjust=1.4, color="white", size=3.5, position = position_dodge(0.9))+
labs(y = "The Sales of Each Brands", x="Years", fill="Brands") +
theme_minimal()