Created
November 27, 2016 17:28
-
-
Save flovv/fd9085cbc70beef6d6f1d39d99b97c2b to your computer and use it in GitHub Desktop.
a unicorn ggplot theme with example charts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(ggplot2) | |
require(ggthemes) | |
require(plyr) | |
require(reshape2) | |
library(grid) | |
require(lubridate) | |
require(stringr) | |
options(stringsAsFactors = FALSE) | |
################### | |
## theme | |
#http://www.color-hex.com/color-palette/22646 | |
pal_unicorn <- c("#d695ef", "#ff7cd8", "#ff42c1", "#6696fd", "#6b41e8") | |
# http://www.color-hex.com/color-palette/12376 | |
pal_unicorn2 <- c("#ffd9d9", "#d4a8a8", "#fd9fe1", "#d0b3cc", "#ffeeee") | |
############# color pieces! | |
scale_fill_unicorn <- function(){ | |
structure(list( | |
scale_fill_manual(values=pal_unicorn) | |
)) | |
} | |
scale_color_discrete_unicorn <- function(){ | |
structure(list( | |
scale_color_manual(values=pal_unicorn) | |
)) | |
} | |
scale_color_continuous_unicorn <- function(){ | |
structure(list( | |
scale_color_gradientn(colours = pal_unicorn) | |
)) | |
} | |
################################## | |
theme_unicorn <- function(base_size=12, font=NA){ | |
txt <- element_text(size = base_size+2, colour = "black", face = "plain") | |
bold_txt <- element_text(size = base_size+2, colour = "black", face = "bold") | |
theme_classic(base_size = base_size, base_family = font)+ | |
theme( | |
###### clean up! | |
legend.key = element_blank(), | |
strip.background = element_blank(), | |
########### text basics | |
text = txt, | |
plot.title = txt, | |
axis.title = txt, | |
axis.text = txt, | |
legend.title = bold_txt, | |
legend.text = txt ) + | |
############## AXIS lines | |
theme( | |
axis.line.y = element_line(colour = "pink", size = 1, linetype = "dashed"), | |
axis.line.x = element_line(colour = "pink", size = 1.2,linetype = "dashed"), | |
#### remove Tick marks | |
axis.ticks=element_blank(), | |
### legend top and no title! | |
legend.position = "top", | |
legend.title = element_blank(), | |
legend.key = element_rect(fill = "lightskyblue1", color = "lightskyblue1"), | |
legend.background = element_rect( fill = "lightskyblue1",color = "pink", size = 0.5,linetype = "longdash"), | |
## background | |
plot.background = element_rect(fill = "lightskyblue1",colour = "pink",size = 0.5, linetype = "longdash") | |
) | |
} | |
#################### | |
unicorns <- read.csv("data/unicorn_data.txt", sep="\t") | |
unicorns$Date.Joined <- as.Date(unicorns$Date.Joined, "%m/%d/%Y") | |
unicorns$year <- format(unicorns$Date.Joined , "%Y") | |
unicorns$Valuation...B. <- as.numeric(str_replace_all(unicorns$Valuation...B., "\\$", "")) | |
dd<- ddply(unicorns, .(year, Industry), summarise, N=length(Industry), m= mean(Valuation...B.) ) | |
ddd<- ddply(unicorns, .(Industry), summarise, m= mean(Valuation...B.) ) | |
ddd <- ddd[order(ddd$m, decreasing = T),] | |
d4<- ddply(unicorns, .(Industry), summarise, N=length(Industry), m= mean(Valuation...B.) ) | |
d4 <- d4[order(d4$m, decreasing = T),] | |
###################### | |
## plots | |
# chart #1 | |
ggplot(dd, aes(year, N, color=Industry, fill=Industry)) + geom_bar(stat="identity") + xlab("") + ylab("Number of Unicorns") + theme_unicorn(base_size = 10) | |
## chart #2 | |
windowsFonts(F = windowsFont('Comic Sans MS')) | |
p <- ggplot(ddd[1:5,], aes(reorder(Industry, -m), m, color=Industry, fill=Industry)) + geom_point(size=4, shape="O") | |
p+ theme_unicorn(base_size = 12,font="F") +scale_color_discrete_unicorn()+scale_fill_unicorn() + ylab("Mean Valuation")+ xlab("Industries") | |
## chart #3 | |
library(png) | |
library(grid) | |
## adding an image | |
img <- readPNG(source = "image/unicorn.png") | |
g <- rasterGrob(img, interpolate=TRUE) | |
p <- ggplot(d4, aes(N, m)) + geom_point(size=2, shape=1, color="Pink") | |
p<- p + theme_unicorn(base_size = 10,font="F") +xlab("")+ ylab("Mean Valuation") + xlab("Number of Unicorns") | |
p <- p + annotation_custom(g, xmin=30, xmax=35, ymin=10, ymax=12) | |
p <- p + annotate(geom="text", x=3.5, y=12, label="Transportation", color="lightblue" , family=F) | |
p <- p + annotate(geom="text", x=12, y=11.5, label="On Demand", color="lightblue") | |
p <- p + annotate(geom="text", x=34, y=4, label="Ecommerce", color="lightblue") | |
p | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment