Skip to content

Instantly share code, notes, and snippets.

@prasanthkothuri
Last active July 27, 2018 23:17
Show Gist options
  • Save prasanthkothuri/cd9e94665f75902f6f3c2bb85e7e7003 to your computer and use it in GitHub Desktop.
Save prasanthkothuri/cd9e94665f75902f6f3c2bb85e7e7003 to your computer and use it in GitHub Desktop.
# read the csv into R dataframe
#url <- "/Users/prasanth/Desktop/amzn.csv"
url <- "https://www.dropbox.com/s/ffq7d5dnb47jzig/amzn.csv?dl=1"
amzn <- read.csv(url,header = TRUE, sep=",")
# Order the dataframe and inspect data
amzn <- amzn[order(amzn$year),]
head(amzn)
# Simple line graph with ggplot2
library(ggplot2)
ggplot(data=amzn, aes(x=year)) +
geom_line(aes(y=share_price, color="share_price"))+
geom_point(aes(y=share_price, color="share_price"))+
scale_x_continuous(labels = amzn$year, breaks = amzn$year)+
labs(title="Amazon revenues & share price, yearly adj. close (1997 - 2018)",
caption="@prasanthkothuri",
y="Share price ($)",
x="Date",
color="")+
geom_line(aes(y=revenue*8, color="revenue"))+
geom_point(aes(y=revenue*8, color="revenue"))+
scale_y_continuous(sec.axis = sec_axis(~./8, name = "Revenue (billions of dollars)"))+
scale_colour_manual(values = c("blue", "red"))+
theme(legend.position = c(0.8, 0.9))
# line and bar plot with dual y axis
library(ggplot2)
ggplot(data=amzn) +
geom_line(aes(x=year, y=share_price, color="share_price"))+
geom_point(aes(x=year, y=share_price, color="share_price"))+
geom_bar(aes(x=year, y=revenue*8, fill="revenue"),stat="identity", alpha=1/2, colour="turquoise")+
scale_x_continuous(labels = amzn$year, breaks = amzn$year)+
labs(title="Amazon revenues & share price, yearly adj. close (1997 - 2018)",
caption="@prasanthkothuri",
y="Share price ($)",
x="Date",
color="Parameter")+
scale_y_continuous(sec.axis = sec_axis(~./8, name = "Revenue (billions of dollars)", breaks = seq(0,300, by=50)),
breaks = seq(0,2000, by=400))+
scale_fill_manual('', labels = 'revenue', values = c("revenue"="turquoise")) +
scale_color_manual('', labels = 'Share Price', values = c("share_price"="blue")) +
theme(legend.position = "bottom",
panel.background = element_rect(fill = 'white'),
panel.grid.major.y = element_line(size=.1, color="grey66")
)
# calculate growth of revenue and share price
library(dplyr)
growth <- function(x)(x/lag(x)-1)*100
amzn <- amzn %>% filter(year > 1999) %>%
mutate_each(funs(growth = growth), revenue, share_price)
# Histogram of amazon revenue growth
ggplot(data = amzn, aes(amzn$revenue_growth)) +
geom_histogram(breaks=seq(0,50,by=5), col="white", aes(fill=..count..),bins=5, binwidth = 5)+
scale_fill_gradient2(low = "darkgreen", mid = "white", high = "darkred")+
labs(title="Histogram of Amazon CAGR between 1997 - 2018",
caption="@prasanthkothuri",
y="Years",
x="Compounded Annual Growth Rate")+
theme(panel.background = element_rect(fill = 'white'),
panel.grid.major.y = element_line(size=.1, color="grey66")
)
# compare revenue growth with share price growth with dual y-axis
ggplot(data=amzn, aes(x=year)) +
geom_line(aes(y=revenue_growth, color="revenue_growth"))+
geom_point(aes(y=revenue_growth, color="revenue_growth"))+
scale_x_continuous(labels = amzn$year, breaks = amzn$year)+
labs(title="Amazon revenues growth vs share price growth (2000 - 2018)",
caption="@prasanthkothuri",
y="Revenue Growth (%)",
x="Date",
color="Legend")+
geom_line(aes(y=share_price_growth/2, color="share_price_growth"))+
geom_point(aes(y=share_price_growth/2, color="share_price_growth"))+
scale_y_continuous(sec.axis = sec_axis(~.*2, name = "Share Price Growth (%)"))+
scale_colour_manual(values = c("blue", "red"))+
theme(legend.position = c(0.8, 0.9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment