-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineplot.r
More file actions
47 lines (27 loc) · 1.27 KB
/
lineplot.r
File metadata and controls
47 lines (27 loc) · 1.27 KB
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
# use line plot when date have a continuity such as time-data.
head(economics)
ggplot(economics, aes(x=date, y=pop)) + geom_line()
# we can use lubridate package to manipulate time data.
require(lubridate)
economics$year <- year(economics$date)
economics$month <- month(economics$date)
head(economics)
econ2000 <- economics[which(economics$year>=2000), ] # this code keeps only data 2000 and after.
nrow(econ2000)
head(econ2000)
econ2000$month <- month(econ2000$date, label = TRUE)
#this code changes the month from numerical to alphabetical.
head(econ2000)
require(scales)
g <- ggplot(econ2000, aes(x=month, y=pop))
g <- g + geom_line(aes(color=factor(year), group=year))
#by setting the year to factor - every year is different color. if not every years would have a different shade of 1 color.
g <- g + geom_line(aes(color=factor(year), group=year))
g
g <- g + scale_color_discrete(name = "Year")
g <- g + scale_y_continuous(labels=comma) # comma is a function in the scales package to format the y-axis.
g <- g +labs(title = "Population Growth", x = 'Month', y= 'Population')
g
# In order to control the angle of the X axis for better visualization.
g <- g + theme(axis.text.x = element_text(angle = 90,hjust = 1))
g