6 Challenges
Let us move to the challenge part of the seminar to examine your understanding. Below are two graphs created by ggplot() based on housedata2.
6.1 Challenge 1: reorder the items in legend
ggplot(data=housedata2, aes(x= yearchi, y=priceper,group=region,color=region)) +geom_point(size=2)+
geom_line(size = 1)+
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=13))+
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))
6.2 Challenge 2: Manualy setting the colour of the line and point
# calculate the rank order of house price in each region at each transaction year.
result1 <- data.frame()
ms <- 0
autodata <-0
j <- 1
list <-unique(housedata2$yearchi)
for(ms in list)
{
print(ms)
print(j)
autodata <- housedata2[housedata2$yearchi==ms,]
autodata$ranknew <- rank(-autodata$priceper)
autodata$ranknew1 <- rank(autodata$priceper)
result1 <- rbind(result1,autodata)
j <- j+1
}
## [1] 2009
## [1] 1
## [1] 2012
## [1] 2
## [1] 2015
## [1] 3
## [1] 2016
## [1] 4
## [1] 2011
## [1] 5
## [1] 2014
## [1] 6
## [1] 2010
## [1] 7
## [1] 2013
## [1] 8
## gor yearchi priceper region ranknew ranknew1
## 1 E12000001 2009 1425.943 North East 9 1
## 13 E12000002 2009 1554.641 North West 7 3
## 24 E12000003 2009 1585.518 Yorkshire and The Humber 6 4
## 32 E12000004 2009 1528.438 East Midlands 8 2
## 35 E12000005 2009 1686.191 West Midlands 5 5
## 44 E12000006 2009 2104.026 East of England 3 7
# Plot house price rank order change between 2009 and 2016
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())