Column

Number of Diamonds in Random Sample

5000

Number of Clarity Categories

8

Number of Colour Categories

7

Number of Cut Categories

5

Diamond Carat Density

Column

Diamond Data

Column

Proportions of Color Within Cut

Price Distributions per Cut

---
title: "Diamonds with flexdashboard"
author: "Udi Alter"
output: 
  flexdashboard::flex_dashboard:
    theme: united
    vertical_layout: fill
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
# if you don't have the following packages installed, please remove the hashtag, run the following lines beginning with "install.packages," and then either delete or add the hashtag in front of them. 

#install.packages("flexdashboard")
#install.packages("tidyverse")
#install.packages("plotly")
#install.packages("MetBrewer")
#install.packages("DT")

# Loading the packages:
library(flexdashboard)
library(tidyverse)
library(plotly)
library(MetBrewer)
library(DT)

# the diamonds dataset comes with ggplot2, but it is very large. For our purposes, we will take a random subsample of size 5000, the code to do this is:
data <- sample_n(diamonds, 5000)

```

Column {data-width=350}
-----------------------------------------------------------------------


### Number of Diamonds in Random Sample

```{r}
# I will first exctract the number of rows in the sample dataset
dnum <- nrow(data)

# I will then use this count in a VALUE BOX
valueBox(dnum, 
         icon = "fa-gem", # this term refers to the diamond icon in Font Awesome
         color = "#a6696f") # This is one of the colours in the Monet palatte
```

### Number of Clarity Categories
```{r}
# extracting number of clarity categories in sample
clrt <- data %>% count(clarity)
  clrtnum <- nrow(clrt)
# using this count in value box
valueBox(clrtnum, 
         icon = "fa-glasses",
         color = "#cb96a1")
```

### Number of Colour Categories
```{r}
clrs <- data %>% count(color)
  clrnum <- nrow(clrs)
valueBox(clrnum, 
         icon = "fa-palette",
         color = "#e9cad0")
```


### Number of Cut Categories

```{r}
cuts <- data %>% count(cut)
  cutnum <- nrow(cuts)
valueBox(cutnum, 
         icon = "fa-scissors",
         color = "#e1dce5")
```



### Diamond Carat Density 
```{r}
# creating the ggplot object first
p1 <- ggplot(data, aes(carat, fill = cut)) +
  geom_density(alpha= 0.8, position = "stack")+scale_fill_manual(values=met.brewer("Monet", 10))+theme_classic()
# conveying it to plotly
ggplotly(p1)
```

Column {data-width=300}
-----------------------------------------------------------------------

### Diamond Data

```{r}
# creating an interactive table
datatable(data)
```


Column {data-width=350}
-----------------------------------------------------------------------

### Proportions of Color Within Cut

```{r, warning=FALSE}

row_pct <- data %>%
  group_by(cut) %>%
  count(color) %>%
  mutate(percent = (n/sum(n)) * 100,
         label = sprintf("%0.0f%%", percent)) # using "%0.1f%%" rounds to one decimal place 

p2 <- ggplot(data, aes(x=cut, fill=color)) + 
  geom_bar(position="fill", aes(text=paste("Cut:", cut, "\nColour:", color))) +
  geom_text(data=row_pct, aes(y=n,label=label),position=position_fill(vjust = 0.5), size = 3) + 
  scale_fill_manual(values=met.brewer("Monet", 7))+
  coord_flip() + theme_classic() + labs(x = "Cut", y = "Proporion")

ggplotly(p2, tooltip = "text")
```

### Price Distributions per Cut

```{r}

p3 <- qplot(cut, price, data = data, geom = "jitter", alpha = I(1/2), color=cut, aes(text=paste("Cut: ", cut, "\nPrice: $",price, "\nCarat: ", carat, "\nColour: ", color,"\nClarity: ", clarity, sep = "")))+scale_color_manual("Cut",values=met.brewer("Monet", 5))+ theme_classic()+ labs(x = "Cut", y = "Price ($US)")+theme(legend.position = "none")
ggplotly(p3, tooltip = "text")

```