Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(ggplot2)
library(RgoogleMaps)
options(scipen=1)
dat.column <- read.csv("column.csv", head=FALSE, fileEncoding="UTF-8", colClasses="character")
dat <- read.csv("data.csv", fileEncoding="UTF-8", colClasses=dat.column[, 3])
dat <- dat[!duplicated(cbind(dat$price, dat$lat, dat$lon)), ]
dat$price[dat$id == "300056150013621013622"] <- NA # 価格の桁間違い?
dat$area[dat$id %in% c("300054130041305041305", "300105420003450004117")] <- NA # 面積の桁間違い?
dat$latitude[dat$id == "300059820006005006005"] <- NA # 緯度不正
dat$longitude[dat$id == "300059820006005006005"] <- NA # 経度不正
dim(dat)
## ----
## pricehist
mean.log.price <- mean(log(dat$price), na.rm=TRUE)
sd.log.price <- sd(log(dat$price), na.rm=TRUE)
breaks <- c(1.25, 2.5, 5, 10, 20, 40) * 10000
g <- ggplot(dat, aes(x=price))
g <- g + scale_x_continuous(
trans="log",
breaks=breaks,
labels=breaks / 10000,
name="価格 (万円)")
g <- g + theme(panel.grid.minor=element_blank())
g <- g + geom_histogram(aes(y=..density..), col=hsv(0.6, 1, 0.75), fill=hsv(0.6, 0.5, 1))
g <- g + stat_function(fun=function(x) dnorm(x, mean.log.price, sd.log.price), size=1, col="blue")
g
## ----
## builthist
dat.building <- dat[!duplicated(cbind(dat$lat, dat$lon)), ]
year <- as.integer(substr(dat.building$built, 1, 4))
g <- ggplot(data.frame(year=na.omit(year)), aes(x=year))
g <- g + scale_x_continuous(breaks=seq(1950, 2010, by=10), name="")
g <- g + geom_histogram(col=hsv(0.6, 1, 0.75), fill=hsv(0.6, 0.5, 1), binwidth=1)
g
## ----
## map
dat.map <- na.omit(dat[, c("price", "latitude", "longitude")])
center <- c(mean(range(dat.map$lat)), mean(range(dat.map$lon)))
map <- GetMap(center, zoom=13)
lv2col <- function(lv, ...) hsv(pmin(pmax(lv, 0), 1) * 0.75, 1, 1, ...)
q <- quantile(log(dat.map$price), c(0.05, 0.95), na.rm=TRUE)
price.level <- (log(dat.map$price) - q[1]) / (q[2] - q[1])
PlotOnStaticMap(map, lat=dat.map$lat, lon=dat.map$lon, pch=3, col=lv2col(price.level))
legend.level <- seq(0, 1, by=0.2)
legend("bottomleft", legend=round(exp(legend.level * (q[2] - q[1]) + q[1])), fill=lv2col(legend.level))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment