# Solution 7
# S plot heatmap ----------------------------------------------------------
x <- interaction(x.bin, y.bin, sep = "_") %>% levels %>% strsplit(split = "_") %>%
unlist %>% as.numeric() %>% matrix(ncol= 2, byrow = T) %>% data.frame
names( x) <- c("x.bin" , "y.bin" )
elevation.df <- data.frame(freq2D = as.vector(freq2D), x.bin = x$x.bin, y.bin = x$y.bin )
elevation.loess <- loess(freq2D ~ x.bin*y.bin, data = elevation.df, degree = 2, span = 0.25) # Predict the gradients
elevation.fit <- expand.grid(list(x.bin = seq(0, 1000, 10), y.bin = seq(0, 700, 10))) # Fit the values in the curve
z <- predict(elevation.loess, newdata = elevation.fit)
elevation.fit$Height <- as.numeric(z)
elevation.fit$Height[ is.na( elevation.fit$Height )] <- 0
colfunc <- colorRampPalette(c("darkblue", "lightblue", "green", "yellow", "red"))
p <- ggplot(elevation.fit, aes(x.bin, y.bin, fill = Height)) + geom_tile() +
geom_point(data= elevation.fit, aes(x = x.bin, y =y.bin), # coordinates
color="black", # Color point
# position=position_jitter(w=0.01,h=0.01), # Point plot desviation
alpha=0.05) +
xlab("X Coordinate") + ylab("Y Coordinate") +
scale_fill_gradientn(colours=colfunc(100)) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0))
print(p)
jpeg(filename = "Results/Solution6.jpeg", width = 1000, height = 700, units = "px")
print(p)
dev.off()
# E plot heatmap ----------------------------------------------------------