generated from jtr13/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path08-Answer.Rmd
115 lines (109 loc) · 5.02 KB
/
08-Answer.Rmd
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Answer
<br>
<br>
Below is the answer to challenge 1:
```{r}
ggplot(data=housedata2, aes(x= yearchi, y=priceper,group=region,color=region)) +geom_point(size=2)+
geom_line(size = 1)+
scale_colour_hue(breaks=c( "London" ,"South East" ,"East of England" ,"South West","West Midlands","Yorkshire and The Humber","East Midlands","North West","North East") )+
theme_bw()+
theme(axis.title = element_text(size=15),
axis.text = element_text(size=13),
strip.text = element_text(size=15),
legend.text = element_text(size=11),
legend.title = element_text(size=15))+
ylab(bquote("House price ( £/"~m^2 ~ ")"))+
xlab("Year")+
guides(color=guide_legend("Region")) +
scale_y_continuous(labels = scales::comma,)+
scale_x_continuous(breaks = c(2009,2010,2011,2012,2013,2014,2015,2016))
```
<br>
You also can change the legend label text by setting the labels using scale_colour_hue.
```{r}
ggplot(data=housedata2, aes(x= yearchi, y=priceper,group=region,color=region)) +geom_point(size=1.8)+
geom_line(size = 0.8)+
scale_colour_hue(breaks=c( "London" ,"South East" ,"East of England" ,"South West","West Midlands","Yorkshire and The Humber","East Midlands","North West","North East"),
labels=c("LD","SE","EE","SW","WM","YH","EM","NW","NE") )+
theme_bw()+
theme(axis.title = element_text(size=15),
axis.text = element_text(size=13),
strip.text = element_text(size=15),
legend.text = element_text(size=11),
legend.title = element_text(size=15))+
ylab(bquote("House price ( £/"~m^2 ~ ")"))+
xlab("Year")+
guides(color=guide_legend("Region")) +
scale_y_continuous(labels = scales::comma,)+
scale_x_continuous(breaks = c(2009,2010,2011,2012,2013,2014,2015,2016))
```
<br>
Below is the answer to challenge 2:
```{r}
ggplot(data=result1,aes(x=yearchi, y=ranknew,group=region,color=region))+
geom_line(aes(color = region, alpha = 1), size = 0.7) +
geom_point(data = result1[(result1$yearchi == "2009"|result1$yearchi == "2016"),],
aes(x=yearchi, y=ranknew,group=region,color = region),
size = 8)+
geom_text(data = result1[result1$yearchi == "2016",],
aes(label = region) ,
position = position_nudge(x = 0.4),
hjust = 0,
size = 5) +
geom_label(data = result1[(result1$yearchi == "2009"|result1$yearchi == "2016"),],
aes(label = ranknew),
color="white",
alpha = 0,
size = 5,
label.padding = unit(0.00, "lines"),
label.size = 0.0)+
theme_bw()+ scale_x_continuous(limits = c(2007, 2021),breaks = c(2009,2010,2011,2012,2013,2014,2015,2016))+
scale_y_reverse( breaks = unique(result1$ranknew))+
theme(legend.position = "none",axis.text = element_text(size=13))+
theme(axis.line=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.minor.y=element_blank(),
plot.background=element_blank())+
scale_colour_manual(values=c("#238443", "#252525","#252525", "#252525","#d95f0e", "#252525","#252525", "#252525","#d95f0e"))
## A more efficient approach
cols <- c("East Midlands" = "#238443","East of England" = "#252525","North East" = "#252525","London" = "#252525","South East" = "#252525","West Midlands" = "#252525","South West" = "#252525", "Yorkshire and The Humber" = "#d95f0e", "North West" = "#d95f0e")
ggplot(data=result1,aes(x=yearchi, y=ranknew,group=region,color=region))+
geom_line(aes(color = region, alpha = 1), size = 0.7) +
geom_point(data = result1[(result1$yearchi == "2009"|result1$yearchi == "2016"),],
aes(x=yearchi, y=ranknew,group=region,color = region),
size = 8)+
geom_text(data = result1[result1$yearchi == "2016",],
aes(label = region) ,
position = position_nudge(x = 0.4),
hjust = 0,
size = 5) +
geom_label(data = result1[(result1$yearchi == "2009"|result1$yearchi == "2016"),],
aes(label = ranknew),
color="white",
alpha = 0,
size = 5,
label.padding = unit(0.00, "lines"),
label.size = 0.0)+
theme_bw()+ scale_x_continuous(limits = c(2007, 2021),breaks = c(2009,2010,2011,2012,2013,2014,2015,2016))+
scale_y_reverse( breaks = unique(result1$ranknew))+
theme(legend.position = "none",axis.text = element_text(size=13))+
theme(axis.line=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.minor.y=element_blank(),
plot.background=element_blank())+
scale_colour_manual(values = cols)
```