In [1]:
library(repr)
options(repr.plot.width=6, repr.plot.height=3.375)
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 [9]:
cutDotAll <- g + aes(cut, "", color = cut) + geom_jitter(size = 1, alpha = 0.1, height = 0.5) + ylab("cut")
cutDotAll
In [10]:
ggsave("../assets/img/cutDotAll.png", cutDotAll, width = 8, height = 2, units = "in", bg = "transparent")
In [11]:
cutDot <- g + aes(cut, color = cut) + geom_point(stat="count", size = 4)
cutDot
In [12]:
ggsave("../assets/img/cutDot.png", cutDot, width = 8, height = 4.5, units = "in", bg = "transparent")
In [13]:
cutBar <- g + aes(cut, fill = cut) + geom_bar()
cutBar
In [14]:
ggsave("../assets/img/cutBar.png", cutBar, width = 8, height = 4.5, units = "in", bg = "transparent")
In [15]:
cutColumn <- g + aes(cut, fill = cut) + geom_bar() + coord_flip()
cutColumn
In [16]:
ggsave("../assets/img/cutColumn.png", cutColumn, width = 8, height = 4.5, units = "in", bg = "transparent")
In [17]:
cutCoxcomb <- g + aes(cut, fill = cut) + geom_bar(width = 1) + coord_polar()
cutCoxcomb
In [18]:
ggsave("../assets/img/cutCoxcomb.png", cutCoxcomb, width = 8, height = 4.5, units = "in", bg = "transparent")
In [19]:
cutStacked <- g + aes(x = "", fill = cut) + geom_bar(width = 0.5) + xlab("cut")
cutStacked
In [20]:
ggsave("../assets/img/cutStacked.png", cutStacked, width = 8, height = 4.5, units = "in", bg = "transparent")
In [21]:
cutBullseye <- g + aes(x = "", fill = cut) + geom_bar(width = 1) + xlab("cut") + coord_polar(theta = "x")
cutBullseye
In [22]:
ggsave("../assets/img/cutBullseye.png", cutBullseye, width = 8, height = 4.5, units = "in", bg = "transparent")
In [23]:
cutPie <- g + aes(x = "", fill = cut) + geom_bar() + xlab("cut") + coord_polar(theta = "y")
cutPie
In [24]:
ggsave("../assets/img/cutPie.png", cutPie, width = 8, height = 4.5, units = "in", bg = "transparent")
In [25]:
summary(diamonds$price)
Out[25]:
In [26]:
priceDot <- g + aes(price, '') + geom_jitter(size = 0.5, alpha = 0.1, height = 0.25, color = PuRdclass4) + ylab('price')
priceDot
In [27]:
ggsave("../assets/img/priceDot.png", priceDot, width = 8, height = 2, units = "in", bg = "transparent")
In [28]:
priceHist <- g + aes(price) + geom_histogram(bins = 400, fill = PuRdclass4)
priceHist
In [29]:
ggsave("../assets/img/priceHist.png", priceHist, width = 8, height = 4.5, units = "in", bg = "transparent")
In [30]:
priceHistLog <- g + aes(log10(price)) + geom_histogram(bins = 400, fill = PuRdclass4)
priceHistLog
In [31]:
priceFreqpoly <- g + aes(log10(price)) + geom_freqpoly(bins = 400, fill = PuRdclass4)
priceFreqpoly
In [32]:
ggsave("../assets/img/priceFreqpoly.png", priceFreqpoly, width = 8, height = 4.5, units = "in", bg = "transparent")
In [33]:
priceDensity <- g + aes(log10(price)) + geom_density(adjust = 1/10, fill = PuRdclass4) + scale_y_continuous(labels = comma)
priceDensity
In [34]:
ggsave("../assets/img/priceDensity.png", priceDensity, width = 8, height = 4.5, units = "in", bg = "transparent")
In [ ]: