##Data-Visualization Home work 1

#Deimantė Jokšaitė

#Task

getwd()
## [1] "C:/Users/skirmantas/Downloads"
setwd("C:/Users/skirmantas/OneDrive/Desktop")
duom <- read.csv2("C:/Users/skirmantas/OneDrive/Desktop/Duomenys/DPP.csv", header = TRUE, sep = ";", dec = ".")
  1. Carat(Weight of Diamond) - Weight of Diamond.
  2. Cut(Quality) - Quality of cut(Fair, Good, Very Good, Premium, Ideal).
  3. Color - Diamond Color(from J -> ‘worst’ to D -> ‘Best’).
  4. Clarity - Measurement of Transparency(how clear the Diamond is) Sequence of clarity ( I1 (worst quality), SI2, SI1, VS2, VS1, VVS2, VVS1, IF(best quality) ).
  5. Table - Width of top of a Diamond.
  6. Price(in US dollars) - Price of Diamond in US dollars.
  7. X(length) - Length of Diamond in mm.
  8. Y(width) - Width of Diamond in mm.
  9. Z(depth) - Depth of Diamond in mm.
  10. Depth - Total depth percentage. It can calculated by a simple formula. Total Depth % = z / mean(x , y) or z * 2 / (x + y).

#Data str()

str(duom)
## 'data.frame':    53940 obs. of  10 variables:
##  $ Carat.Weight.of.Daimond.: num  0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
##  $ Cut.Quality.            : chr  "Ideal" "Premium" "Good" "Premium" ...
##  $ Color                   : chr  "E" "E" "E" "I" ...
##  $ Clarity                 : chr  "SI2" "SI1" "VS1" "VS2" ...
##  $ Depth                   : num  61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
##  $ Table                   : num  55 61 65 58 58 57 57 55 61 61 ...
##  $ Price.in.US.dollars.    : int  326 326 327 334 335 336 336 337 337 338 ...
##  $ X.length.               : num  3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
##  $ Y.width.                : num  3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
##  $ Z.Depth.                : num  2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
#install.packages("ggpubr")
library(ggpubr)
## Įkeliamas reikalingas paketas: ggplot2
library(ggridges)
## Warning: paketas 'ggridges' buvo sukurtas pagal R versijà 4.1.3
library(ggplot2)
library(viridis)
## Įkeliamas reikalingas paketas: viridisLite
## Warning: paketas 'viridisLite' buvo sukurtas pagal R versijà 4.1.3
library(hrbrthemes)
## Warning: paketas 'hrbrthemes' buvo sukurtas pagal R versijà 4.1.3
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
ggplot(data = duom, aes(x = Price.in.US.dollars., y = Clarity, fill = Price.in.US.dollars. ),) +
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
  scale_fill_viridis(name = "Price.in.US.dollars.", option = "C") +
  labs(title = 'Clarity by prices') +
  theme_ipsum() +
  theme(
    legend.position="none", 
    panel.spacing = unit(0.1, "lines"),
    strip.text.x = element_text(size = 8)
  )
## Picking joint bandwidth of 403
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): ðrifto ðeima
## nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): ðrifto ðeima
## nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): ðrifto ðeima
## nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## ðrifto ðeima nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

duom %>%
  ggplot( aes(x=Cut.Quality., y=Depth , fill=Cut.Quality.)) +
  geom_violin() +
  scale_fill_viridis(discrete = TRUE, alpha=0.6, option="A") +
  theme_ipsum() +
  theme(
    legend.position="none",
    plot.title = element_text(size=11)
  ) +
  ggtitle("Violin chart by cut quality ") +
  xlab("")
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): ðrifto ðeima
## nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## ðrifto ðeima nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

duom %>%
  ggplot( aes(x=Color, y = Carat.Weight.of.Daimond., fill=Color)) +
  geom_boxplot() +
  scale_fill_viridis(discrete = TRUE, alpha=0.6, option="A") +
  theme_ipsum() +
  theme(
    legend.position="none",
    plot.title = element_text(size=11)
  ) +
  ggtitle("Boxplot by color") +
  xlab("")
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## ðrifto ðeima nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

duom %>%
  ggplot( aes(x=Clarity, y = Depth, fill=Clarity)) +
  geom_boxplot() +
  scale_fill_viridis(discrete = TRUE, alpha=0.6, option="A") +
  theme_ipsum() +
  theme(
    legend.position="none",
    plot.title = element_text(size=11)
  ) +
  ggtitle("Boxplot by depth") +
  xlab("")
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## ðrifto ðeima nerasta Windows ðriftø duomenø bazëje
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : ðrifto
## ðeima nerasta Windows ðriftø duomenø bazëje

theme_set(theme_classic())

# Plot
g <- ggplot(duom, aes(Price.in.US.dollars.))
g + geom_density(aes(fill=factor(Color)), alpha=0.8) + 
  labs(title="Density plot", 
       subtitle="Color Grouped by Price",
       caption="Source: duom",
       x="Price",
       fill="# Clarity")

df <- as.data.frame(table(duom$Clarity))
colnames(df) <- c("Clarity", "freq")
pie <- ggplot(df, aes(x = "", y=freq, fill = factor(Clarity))) + 
  geom_bar(width = 1, stat = "identity") +
  theme(axis.line = element_blank(), 
        plot.title = element_text(hjust=0.5)) + 
  labs(fill="Clarity", 
       x=NULL, 
       y=NULL, 
       title="Pie Chart of class", 
       caption="Source: duom")

pie + coord_polar(theta = "y", start=0)

freqtable <- table(duom$Cut.Quality. )
df <- as.data.frame.table(freqtable)
str(duom)
## 'data.frame':    53940 obs. of  10 variables:
##  $ Carat.Weight.of.Daimond.: num  0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
##  $ Cut.Quality.            : chr  "Ideal" "Premium" "Good" "Premium" ...
##  $ Color                   : chr  "E" "E" "E" "I" ...
##  $ Clarity                 : chr  "SI2" "SI1" "VS1" "VS2" ...
##  $ Depth                   : num  61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
##  $ Table                   : num  55 61 65 58 58 57 57 55 61 61 ...
##  $ Price.in.US.dollars.    : int  326 326 327 334 335 336 336 337 337 338 ...
##  $ X.length.               : num  3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
##  $ Y.width.                : num  3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
##  $ Z.Depth.                : num  2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
g <- ggplot(df, aes(Var1, Freq))
g + geom_bar(stat="identity", width = 0.5, fill="purple") + 
  labs(title="Bar Chart", 
       subtitle="Cut quality") +
  theme(axis.text.x = element_text(angle=65, vjust=0.6))