Libraries
library(tidyverse)
library(scales)
library(ggrepel)
library(plotly)
Setting work directory and importing data
setwd("E:/Writting/Visualization")
data<-read.csv("dataset_price_personal_computers_Chaptr6.csv")
#unselecting the first column
data<-data %>%
select(-X)
Converting characters into levels/Factors
data$cd<-as.factor(data$cd)
data$multi<-as.factor(data$multi)
data$premium<-as.factor(data$premium)
data$ram<-as.factor(data$ram)
data$screen<-as.factor(data$screen)
Pie Chart
#labelling RAM as Factors i.e 4GB 8GB & 16GB
data<-data %>%
mutate(ram=recode_factor(ram,"2"= "2GB",
"4"= "4GB",
"8"= "8GB",
"16"= "16GB",
"24"= "24GB",
"32"= "32GB",
"64"= "64GB",
"128"= "128GB",
"512"= "512GB")) %>%
arrange(ram)
#grouping ram values
pie<-data %>%
group_by(ram) %>%
summarise(count=n()) %>%
arrange(count)
#calculating percentages
pie$percent<-round(prop.table(pie$count)*100,2)
pie<-pie %>%
mutate(cumulative=round(cumsum(percent)-0.5*percent,2))
#pie chart code
pie %>%
ggplot(aes(x="",y=percent,fill=ram))+
geom_bar(width = 1,stat="identity",color="white")+
coord_polar("y")+
theme_void()+
labs(title = "Computer Ram")+
theme(plot.title = element_text(hjust = 0.5))
Bar Graph
#grouping the screens by inches
screen<-data %>%
group_by(screen) %>%
summarise(count_bar=n()) %>%
arrange(desc(count_bar))
#ploting the bar graph
screen %>%
ggplot(aes(screen,count_bar))+
geom_bar(stat="identity",fill="steelblue")+
theme_minimal()+
labs(title = "Computer Screen Sizes",
x="Screen Sizes",
y="Frequency")+
theme(plot.title = element_text(hjust = 0.5))+
geom_label(aes(label=count_bar),vjust=1.1,cex=2.5)
Histogram
data %>%
ggplot(aes(price))+
geom_histogram(bins = 50,color="white",fill="steelblue")+
theme_minimal()+
labs(title = "Computer Prices",
x="Prices")+
theme(plot.title = element_text(hjust = 0.5),
axis.title.y = element_blank())
Boxplot
data %>%
ggplot(aes(premium,price,fill=premium))+
geom_boxplot(outlier.shape = 18,outlier.alpha = 0.4)+
theme_minimal()+
scale_color_manual(values = c('#999999','#E69F00'))+
labs(title = "Premium Computer Prices Distribution",
x="Premium",
y="Price")+
theme(plot.title = element_text(hjust = 0.5),
legend.position = "bottom")
Scatter Plot
t<-data %>%
ggplot(aes(hd,price,color=hd))+
geom_point(position = "jitter")+
theme_minimal()+
labs(title = "Computer Price vs Hard Drive Size",
x="Hard Drive Size",
y="Price")+
theme(plot.title = element_text(hjust = 0.5),
legend.position = "none",
axis.line = element_line(),
axis.ticks = element_line())
ggplotly(t)