options(scipen=999)
library(tidyverse)
library(socviz)
library(datasetsICR)LAB 5
PART 1: PRACTICE USING PIPES (dplyr) TO SUMMARIZE DATA: TWO CATEGORICAL VARIABLES
library(tidyverse)
library(ggplot2)
library(dplyr)
head(diamonds)# A tibble: 6 × 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
pip1 <- diamonds %>%
group_by(cut, clarity) %>%
summarize(N = n()) %>%
mutate(freq = N/sum(N),
pct = round((freq*100),0))
pip1# A tibble: 40 × 5
# Groups: cut [5]
cut clarity N freq pct
<ord> <ord> <int> <dbl> <dbl>
1 Fair I1 210 0.130 13
2 Fair SI2 466 0.289 29
3 Fair SI1 408 0.253 25
4 Fair VS2 261 0.162 16
5 Fair VS1 170 0.106 11
6 Fair VVS2 69 0.0429 4
7 Fair VVS1 17 0.0106 1
8 Fair IF 9 0.00559 1
9 Good I1 96 0.0196 2
10 Good SI2 1081 0.220 22
# … with 30 more rows
PART 2: CREATE STACKED AND DODGED BAR CHARTS FROM 2 CATEGORICAL VARIABLES
p_title <- "Cut of Diamond by Clarity"
p_caption <- "ggplot2 Diamonds dataset"
# AS STACKED BAR CHART
p <- ggplot(data = subset(pip1, !is.na(cut) & !is.na(clarity)),
aes(x=cut, y=pct, fill = clarity))
p + geom_col(position = "stack") +
labs(x="Type of Cut", y="Percent", fill = "Clarity",
title = p_title, caption = p_caption,
subtitle = "As a stacked bar chart") +
geom_text(aes(label=pct), position = position_stack(vjust=.5))# AS DODGED BAR CHART
p + geom_col(position = "dodge2") +
labs(x="Type of Cut", y="Percent", fill = "Clarity",
title = p_title, caption = p_caption,
subtitle = "As a dodged bar chart") +
geom_text(aes(label = pct), position = position_dodge(width = .9)) # AS FACETED HORIZONTAL BAR CHART
p + geom_col(position = "dodge2") +
labs(x=NULL, y="Percent", fill = "Clarity",
title = p_title, caption = p_caption,
subtitle = "As a faceted horizontal bar chart") +
guides(fill = "none") +
coord_flip() +
facet_grid(~ cut) +
geom_text(aes(label = pct), position = position_dodge2(width = 1))PART 3: PRACTICE USING PIPES (dplyr) TO SUMMARIZE DATA: TWO CONTINUOUS & ONE CATEGORICAL VARIABLE
pip2 <- diamonds %>%
group_by(cut) %>%
summarize(N = n(),
carat_mean = mean(carat, na.rm=TRUE),
depth_mean = mean(depth, na.rm=TRUE)) %>%
mutate(freq = N/sum(N),
pct = round((freq*100),0))
pip2# A tibble: 5 × 6
cut N carat_mean depth_mean freq pct
<ord> <int> <dbl> <dbl> <dbl> <dbl>
1 Fair 1610 1.05 64.0 0.0298 3
2 Good 4906 0.849 62.4 0.0910 9
3 Very Good 12082 0.806 61.8 0.224 22
4 Premium 13791 0.892 61.3 0.256 26
5 Ideal 21551 0.703 61.7 0.400 40
PART 4: SCATTERPLOT WITH A THIRD CATEGORICAL VARIABLE
This code does not remove NA – make sure you deal with that.
p <- ggplot(pip2, aes(x=carat_mean, y=depth_mean, color=cut))
p + geom_point(size=5) +
annotate(geom = "text", x = .6, y=58,
label = "Usually the better the cut the deppest and highest carat the diamond has.", hjust=0) +
labs(y="Average Carat", x="Average Depth.",
title="Carat and Depth by Cut",
subtitle = "Fair type of cut has the higher average carat and depth",
caption <- "ggplot2 dataset{diamonds}")PART 5: LEGEND AND GUIDES
pip2$cut.c <- as.character(pip2$cut)
pip2 <- pip2[order(pip2$cut.c),]
pip2 # A tibble: 5 × 7
cut N carat_mean depth_mean freq pct cut.c
<ord> <int> <dbl> <dbl> <dbl> <dbl> <chr>
1 Fair 1610 1.05 64.0 0.0298 3 Fair
2 Good 4906 0.849 62.4 0.0910 9 Good
3 Ideal 21551 0.703 61.7 0.400 40 Ideal
4 Premium 13791 0.892 61.3 0.256 26 Premium
5 Very Good 12082 0.806 61.8 0.224 22 Very Good
p <- ggplot(pip2, aes(x=carat_mean, y=depth_mean, color=cut.c))
p + geom_point(size=5) +
annotate(geom = "text", x = .6, y=58,
label = "Usually the better the cut the deppest and highest carat the diamond has.", hjust=0) +
labs(y="Average Carat", x="Average Depth",
color = "Cut") +
theme(legend.title = element_text(color="gray50", size=10, face="bold"),
legend.position = c(x=0.9, y=.7)) PART 6: DATA LABELS VS LEGEND
p <- ggplot(pip2, aes(x=carat_mean, y=depth_mean, color=cut.c))
p + geom_point(size=5) +
geom_text(mapping = aes(label=cut), hjust=-.4, size=3) +
annotate(geom = "text", x = .6, y=50,
label = "Usually the better the cut the lowest depth and carat the diamond has.", hjust=0) +
labs(y="Average Carat", x="Average Depth",
title="here",
color = "Type of Cut") +
theme(legend.position = "none")PART 7: INTERPRETATION
The data set provided with different attributes for different diamonds. I used the two categorical variables of cut and clarity and the two numerical variables of carat and depth. As the visualizations showed the better the cut, the diamond used to have a lowest carat and depth. It was interesting to see that the only one that did not follow this pattern was the “Good” and “Very Good” type of cut, because the first was higher than the second one. Most of types of cut are between more than 60 in carat and 1.1 in depth. The average are reasonably close to each other and it does not show any outliers. The information could be useful for any interested in purchasing diamonds or starting a business related with them.