library(dplyr)
library(lubridate)
library(highcharter)
data(txhousing, package = "ggplot2")
str(txhousing)
incomplete_series <- filter(txhousing, is.na(median)) %>% distinct(city)
my_data <- txhousing %>%
anti_join(incomplete_series)
df <- my_data %>%
mutate(Price = median/1000) %>%
mutate(Period = make_date(year, month, 1)) %>%
select(City = city, Year = year, Month = month, Period, Price)
hchart(df, 'line', hcaes(x = 'Period', y = 'Price', group = "City")) %>%
hc_title(text = "Monthly Housing Price in Texan Cities",
margin = 20, align = "center",
style = list(color = "black", fontWeight = "bold")) %>%
hc_yAxis(title = list(text = "Price (K$)")) %>%
hc_xAxis(title = list(text = NULL))
agg_city <- df %>%
group_by(City) %>%
summarise(Indicator = mean(Price))
top_limit = 8
tpn_cities <- agg_city %>%
top_n(top_limit, Indicator)
btn_cities <- agg_city %>%
top_n(-top_limit, Indicator)
df1 <- df %>%
inner_join(tpn_cities) %>%
arrange(desc(Indicator), City, Period) %>%
mutate(City = factor(City, unique(City)))
df2 <- df %>%
inner_join(btn_cities) %>%
arrange(desc(Indicator), City, Period) %>%
mutate(City = factor(City, unique(City)))
title_caption = paste("Monthly Housing Price of Top", top_limit, "Cities")
p1 <- hchart(df1, 'line', hcaes(x = 'Period', y = 'Price', group = "City")) %>%
hc_title(text = title_caption,
margin = 20, align = "center",
style = list(color = "black", fontWeight = "bold")) %>%
hc_yAxis(title = list(text = "Price (K$)")) %>%
hc_xAxis(title=list(text = NULL))
title_caption = paste("Monthly Housing Price of Bottom", top_limit, "Cities")
p2 <- hchart(df2, 'line', hcaes(x = 'Period', y = 'Price', group = "City")) %>%
hc_title(text = title_caption,
margin = 20, align = "center",
style = list(color = "black", fontWeight = "bold")) %>%
hc_yAxis(title = list(text = "Price (K$)")) %>%
hc_xAxis(title = list(text = NULL))
library(htmltools)
p <- hw_grid(p1, p2)
browsable(p)
seasonal <- df %>%
group_by(Year, Month) %>%
summarise(Price = mean(Price))
hchart(seasonal, 'line', hcaes(x = 'Month', y = 'Price', group = "Year")) %>%
hc_yAxis(title = list(text = "Average house price (K$)")) %>%
hc_xAxis(tickInterval = 1) %>%
hc_title(text = "Monthly Housing Price - Seasonal plot",
margin = 20, align = "center",
style = list(color = "black", fontWeight = "bold")) %>%
hc_tooltip(headerFormat = "<b>Year: {series.name}</b><br><b>Month: {point.key}</b><br>",
pointFormat = "Average house price (K$): {point.y:,.2f}")
top_limit = 3
df3 <- df %>%
group_by(Month) %>%
summarise(Bar_size = mean(Price)) %>%
top_n(top_limit, Bar_size) %>%
mutate(Month_name = month(make_date(2019, Month, 1), label=TRUE, abbr=FALSE))
title_caption = paste("Housing Price - Top", top_limit, "Months")
hchart(df3, "column", hcaes(x = Month_name, y = Bar_size)) %>%
hc_xAxis(title=list(text = NULL), tickLength = 0,
lineWidth = 3, lineColor = "black",
labels = list(style = list(fontSize = "14px", fontWeight = "bold"))) %>%
hc_yAxis(title = list(text = "Average house price (K$)")) %>%
hc_title(text = title_caption,
margin = 40, align = "center",
style = list(fontWeight = "bold")) %>%
hc_tooltip(headerFormat = "<b>{point.key}</b><br>",
pointFormat = "Average house price (K$): {point.y:,.2f}")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
35 | |
25 | |
14 | |
7 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |