Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Last active October 14, 2018 19:15
Show Gist options
  • Save mschnetzer/0aaae9db3121bc50b84d0235cdb9eebb to your computer and use it in GitHub Desktop.
Save mschnetzer/0aaae9db3121bc50b84d0235cdb9eebb to your computer and use it in GitHub Desktop.
Baugrundstückspreise nach Bezirken, 2017
# Download XLS, merge sheets and export as csv: http://www.statistik.at/web_de/statistiken/wirtschaft/preise/immobilien_durchschnittspreise/index.html
immodat <- read_csv2("baugrundstueckspreise_2017.csv",skip = 3) %>%
select(iso=B.Nr., preis=starts_with("Euro")) %>% add_row(iso=900,preis=1000) %>%
filter(!is.na(iso), !is.na(preis)) %>% mutate(preis=round(preis,-1)) %>%
mutate(preis=cut(preis,breaks=c(0,50,100,200,500,3000),
labels=c("<50€","50-100€","100-200€","200-500€",">500€")))
source("plotmap.R")
plotbezirke(dataset=immodat,fillvar="preis",wienbezirke=F,colpal=msc_palette[5:1],
tit="Baugrundstückspreise in Österreich",subtit="Durchschnittspreis pro Quadratmeter nach politischen Bezirken, 2017",
captit="Daten: Statistik Austria, Grafik: @matschnetzer",
savfile="grundstückspreise.png",
legpos="right")
plotbezirke <- function(dataset,fillvar,colpal,wienbezirke=T,tit,subtit,captit,savfile,legpos="none"){
require(sf)
require(tidyverse)
require(ggrepel)
require(msthemes)
# Load JSON map from https://github.com/ginseng666/GeoJSON-TopoJSON-Austria
map <- ifelse(wienbezirke==T, "bezirke_wien_gross_geo.json","bezirke_999_geo.json")
geodat <- st_read(map, quiet=TRUE, stringsAsFactors=FALSE) %>%
mutate(
center = map(geometry, st_centroid),
centercoord = map(center, st_coordinates),
ccordx = map_dbl(centercoord, 1),
ccordy = map_dbl(centercoord, 2)
) %>%
mutate(iso = as.numeric(iso)) %>%
mutate(name = str_replace_all(name,c("\\(Stadt\\)"="","-Stadt"=""," Stadt"=""," am Wörthersee"=""))) %>%
mutate(labselect=as.factor(iso %in% c(101,201,302,401,501,601,701,802,900)))
labels <- data.frame(iso = c(802,701,501,401,302,101,601,201,900),
nudgey=c(0.4,-0.5,0.1,0.5,1.2,-0.5,-1.5,-1.7,0.1),
nudgex=c(0,0.2,-0.6,0,0.6,1,0,-0.8,0.9))
df <- geodat %>% left_join(labels,by="iso") %>% left_join(immodat,by="iso")
df %>%
ggplot() +
geom_sf(aes(fill=get(paste(fillvar)))) +
coord_sf(datum=NA) + # datum=NA to supress long and lat labels
geom_label_repel(data=subset(df,labselect==TRUE),aes(x = ccordx,y = ccordy,label = name),
color="black",size=2.5,nudge_y=df$nudgey[df$labselect==TRUE],
nudge_x=df$nudgex[df$labselect==TRUE],segment.size = 0.3) +
scale_fill_manual(values=colpal) +
theme_ms(alttf = F) + theme(legend.title = element_blank(), legend.position = legpos) +
labs(x="",y="",title=tit,subtitle=subtit,caption=captit) +
ggsave(savfile)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment