In [1]:
library(repr)
options(repr.plot.width=8, repr.plot.height=4.5)
options(jupyter.plot_mimetypes = 'image/png')
In [2]:
# Load the libraries
library(ggplot2)
library(scales)
In [3]:
# Modify the bw theme
theme_viz <- theme_bw() + theme(
legend.background = element_rect(fill = "transparent", colour = NA),
axis.line = element_line(colour = "grey85"),
panel.background = element_rect(fill = "transparent", colour = NA),
panel.border = element_rect(colour = NA),
plot.background = element_rect(fill = "transparent", colour = NA))
In [4]:
# Set it as the defualt
theme_set(theme_viz)
In [5]:
# ColorBrewer class for palette PuRd
# http://colorbrewer2.org/?type=sequential&scheme=PuRd&n=7
PuRdclass1 <- "#f1eef6"
PuRdclass2 <- "#d4b9da"
PuRdclass3 <- "#c994c7"
PuRdclass4 <- "#df65b0"
PuRdclass5 <- "#e7298a"
PuRdclass6 <- "#ce1256"
PuRdclass7 <- "#91003f"
In [6]:
str(diamonds)
In [7]:
# Get the summary for diamonds$cut
summary(diamonds$cut)
Out[7]:
In [8]:
# Set the defualt scale and dataset
g <- ggplot(diamonds) + scale_fill_brewer(palette = "PuRd") + scale_color_brewer(palette = "PuRd")
In [21]:
cutClarityDot <- g + aes(cut, clarity, color = clarity) + geom_jitter(size = 1, alpha = 0.2)
cutClarityDot
In [22]:
ggsave("../assets/img/cutClarityDot.png", cutClarityDot, width = 8, height = 4.5, units = "in", bg = "transparent")
In [15]:
cutClarityBarStack <- g + aes(cut, fill = clarity) + geom_bar(position = 'stack')
cutClarityBarStack
In [12]:
ggsave("../assets/img/cutClarityBar.png", cutClarityBar, width = 8, height = 4.5, units = "in", bg = "transparent")
In [14]:
cutClarityBarDodge <- g + aes(cut, fill = clarity) + geom_bar(position = "dodge")
cutClarityBarDodge
In [16]:
ggsave("../assets/img/cutClarityBarDodge.png", cutClarityBarDodge, width = 8, height = 4.5, units = "in", bg = "transparent")
In [15]:
cutCoxcomb <- g + aes(cut, fill = cut) + geom_bar(width = 1) + coord_polar()
cutCoxcomb
In [16]:
ggsave("../assets/img/cutCoxcomb.png", cutCoxcomb, width = 8, height = 4.5, units = "in", bg = "transparent")
In [17]:
cutStacked <- g + aes(x = "", fill = cut) + geom_bar(width = 0.5) + xlab("cut")
cutStacked
In [18]:
ggsave("../assets/img/cutStacked.png", cutStacked, width = 8, height = 4.5, units = "in", bg = "transparent")
In [19]:
cutBullseye <- g + aes(x = "", fill = cut) + geom_bar(width = 1) + xlab("cut") + coord_polar(theta = "x")
cutBullseye
In [20]:
ggsave("../assets/img/cutBullseye.png", cutBullseye, width = 8, height = 4.5, units = "in", bg = "transparent")
In [21]:
cutPie <- g + aes(x = "", fill = cut) + geom_bar() + xlab("cut") + coord_polar(theta = "y")
cutPie
In [22]:
ggsave("../assets/img/cutPie.png", cutPie, width = 8, height = 4.5, units = "in", bg = "transparent")
In [23]:
summary(diamonds$price)
Out[23]:
In [24]:
priceDot <- g + aes('', price) + geom_jitter(size = 1, alpha = 0.1, width = 0.25, color = PuRdclass4) +
coord_flip() + xlab('price')
priceDot
In [25]:
ggsave("../assets/img/priceDot.png", priceDot, width = 8, height = 4.5, units = "in", bg = "transparent")
In [45]:
priceHist <- g + aes(price) + geom_histogram(bins = 400, fill = PuRdclass4)
priceHist
In [27]:
ggsave("../assets/img/priceHist.png", priceHist, width = 8, height = 4.5, units = "in", bg = "transparent")
In [50]:
priceHistLog <- g + aes(log10(price)) + geom_histogram(bins = 400, fill = PuRdclass4)
priceHistLog
In [52]:
priceFreqpoly <- g + aes(log10(price)) + geom_freqpoly(bins = 400, fill = PuRdclass4)
priceFreqpoly
In [29]:
ggsave("../assets/img/priceFreqpoly.png", priceFreqpoly, width = 8, height = 4.5, units = "in", bg = "transparent")
In [53]:
priceDensity <- g + aes(log10(price)) + geom_density(adjust = 1/10, fill = PuRdclass4) + scale_y_continuous(labels = comma)
priceDensity
In [31]:
ggsave("../assets/img/priceDensity.png", priceDensity, width = 8, height = 4.5, units = "in", bg = "transparent")
In [ ]: