-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sharp Sight Labs - How to make a line chart with ggplot2.R
67 lines (54 loc) · 2.08 KB
/
Sharp Sight Labs - How to make a line chart with ggplot2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
## Tutorial http://sharpsightlabs.com/blog/line-chart-ggplot2-amzn/
#===============
# LOAD PACKAGES
#===============
library(readr) ## readr, which we’ll use to read in the data
library(tidyverse)
library(stringr) ## let us do some string manipulation
#==========
# READ DATA
#==========
stock_amzn <- read_csv("http://sharpsightlabs.com/wp-content/uploads/2017/09/AMZN_stock.csv")
stock_amzn %>% names()
stock_amzn %>% head()
#=========================================================
# CHANGE COLUMN NAMES: lower case
# - in the raw form (as read in) the first letter of
# each variable is capitalized.
# - This makes them harder to type! Not ideal.
# - we'll use stringr::str_to_lower() to change the column
# names to lower case
#=========================================================
colnames(stock_amzn) <- colnames(stock_amzn) %>% str_to_lower()
# inspect
stock_amzn %>% names()
#======
# PLOT
#======
#--------------------------------------
# FIRST ITERATION
# - this is the quick-and-dirty version
#--------------------------------------
ggplot(data = stock_amzn, aes(x = date, y = close)) +
geom_line()
#--------------------------------------
# POLISHED VERSION
# - this is the 'finalized' version
# - we arrive at this after a lot of
# itteration ....
#--------------------------------------
ggplot(stock_amzn, aes(x = date, close)) +
geom_line(color = 'cyan') +
geom_area(fill = 'cyan', alpha = .1) +
labs(x = 'Date'
, y = 'Closing\nPrice'
, title = "Amazon's stock price has increased dramatically\nover the last 20 years") +
theme(text = element_text(family = 'Gill Sans', color = "#444444")
,panel.background = element_rect(fill = '#444B5A')
,panel.grid.minor = element_line(color = '#4d5566')
,panel.grid.major = element_line(color = '#586174')
,plot.title = element_text(size = 28)
,axis.title = element_text(size = 18, color = '#555555')
,axis.title.y = element_text(vjust = 1, angle = 0)
,axis.title.x = element_text(hjust = 0)
)