Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(png)
require(magick)
require(ggplot2)
require(ggimage)
require(ggrepel)
library(ggpubr)
```
```{r}
source("read_wapData.R")
```
```{r}
test_hill <- c(42.72861190404861, -73.67421283293787)
max_coordinate_value_x <- 10
max_coordinate_value_y <- 10
max_distance <- 0.001
grob_length_x <- 2
grob_length_y <- 3
min_buffer_x <- 0.1
min_buffer_y <- 0.1
starter_y_val <- 5
Map_to_x <- function(x, max_coordinate_value){ max(x, 1+x-floor(1+x) ) * max_coordinate_value }
shift_y_2 <- function(test_final_matrix, grob_length_y, min_buffer_y, starter_y_val){
sequencevals <- rep(-1:1, times = floor(nrow(test_final_matrix)/3)+1)*(min_buffer_y+grob_length_y) +
starter_y_val
sequence_trimmed = sequencevals[1:nrow(test_final_matrix)]
final_matrix = test_final_matrix %>% mutate("yloc" = sequence_trimmed)
final_matrix
}
correct_bounds_2 <- function(test_final_matrix, grob_length_x, max_coordinate_value, min_buffer_x){
sorted_matrix <- test_final_matrix[order(test_final_matrix$xloc),]
bottom_matrix <- sorted_matrix[sorted_matrix$xloc< grob_length_x/2+min_buffer_x ,]
bottom_matrix$xloc <- bottom_matrix$xloc + grob_length_x/2 + min_buffer_x - bottom_matrix[1,2]
top_matrix <- sorted_matrix[sorted_matrix$xloc> max_coordinate_value - (grob_length_x/2+min_buffer_x) ,]
top_matrix$xloc <- top_matrix$xloc + max_coordinate_value - grob_length_x/2 - min_buffer_x - top_matrix[nrow(top_matrix),2]
middle_matrix = sorted_matrix[sorted_matrix$xloc> grob_length_x/2+min_buffer_x ,]
middle_matrix = middle_matrix[middle_matrix$xloc< max_coordinate_value - (grob_length_x/2+min_buffer_x) ,]
final_matrix = rbind.data.frame(bottom_matrix, middle_matrix, top_matrix)
final_matrix = final_matrix[order(final_matrix$xloc),]
final_matrix
}
Get_x_coordinates <- function(Lat, Lng, max_distance, rpi_wap_last7 = rpi_wap_last7, max_coordinate_value = 10,
grob_length_x=2, grob_length_y=3, min_buffer_x = 0 ,min_buffer_y = 0 , starter_y_val = 5.5){
true_max_coordinate_value = max_coordinate_value-1
position = c(Lng,Lat)
building_coords <- as.matrix(buildinginfo %>% select(c(3,2)) )
rownames(building_coords) <- t(buildinginfo %>% select(1))
colnames(building_coords) <- c("lng","lat" )
signed_building_coords <- scale(building_coords, center = position, scale = FALSE)
distance_matrix <- as.matrix(signed_building_coords[,1]^2+signed_building_coords[,2]^2)^(1/2)
tot_building_info <- data.frame(signed_building_coords) %>% mutate("Distance" = distance_matrix)
select_building_info <- tot_building_info %>% filter(Distance < max_distance)
unit_coords_lng <- matrix(select_building_info$lng/select_building_info$Distance)
unit_coords_lat <- matrix( select_building_info$lat /select_building_info$Distance )
unit_coords <- data.frame(unit_coords_lng, unit_coords_lat)
signed_fraction <- acos(unit_coords[,2]) * sign(unit_coords[,1])/(2*pi)
unit_x <- as.numeric(lapply(signed_fraction, Map_to_x, max_coordinate_value = true_max_coordinate_value) )
final_df <- data.frame("Buildings" = row.names(select_building_info), "xloc" = unit_x, "yloc" = starter_y_val)
final_df <- correct_bounds_2(final_df, grob_length_x, true_max_coordinate_value, min_buffer_x)
final_df$xloc <- final_df$xloc+1
final_df$yloc <- final_df$yloc+1
final_df <- shift_y_2(final_df, grob_length_y, min_buffer_y, starter_y_val)
return(final_df)
}
testans <-Get_x_coordinates( test_hill[1], test_hill[2],max_distance)
testans
ggplot(data = data.frame(testans), aes(x = xloc, y = yloc, color = Buildings)) + geom_point(size = 2) +
xlim(1, max_coordinate_value_x)+ylim(1, max_coordinate_value_y)+
geom_hline(yintercept = max_coordinate_value_y, alpha = 0.5) +
geom_hline(yintercept = 1, alpha = 0.5) +
geom_vline(xintercept = max_coordinate_value_x, alpha = 0.5) +
geom_vline(xintercept = 1, alpha = 0.5) +
theme_classic2()
```
```{r}
name = c("Freshman Hill", "Sophomore Quad", "86 Field", "East Campus Athletic Village")
lat = c(42.728625,42.730407,42.730380,42.731488)
lng = c(-73.674214,-73.680209,-73.678015,-73.668876)
Test_DF = data.frame("Building" = name, "Lat" = lat, "Lng" = lng)
Function_results = mapply(Get_x_coordinates,Test_DF$Lat,Test_DF$Lng, 0.001)
Function_results
```
```{r}
df <- data.frame(x= c(1:10), y = c(1:10))
df2 <- data.frame(x = 1 , y = 1)
g <- ggplotGrob(ggplot(df2, aes(x, y)) + geom_point() +
theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank(),
axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()))
df
testplot <- function(data, Df = df,Inset = g){
Base <- ggplot(Df, aes(x,y)) + geom_blank() + theme_bw() + coord_fixed(ratio = 1, c(0,10), c(0,10))+
geom_hline(yintercept = max_coordinate_value_y, alpha = 0.5) +
geom_hline(yintercept = 1, alpha = 0.5) + geom_vline(xintercept = 1, alpha = 0.5) +
geom_vline(xintercept = max_coordinate_value_x, alpha = 0.5)
for (i in 1:nrow(data)){
Base <- Base + annotation_custom(grob = Inset, xmin = data[i,2]-grob_length_x/2, xmax = data[i,2]+grob_length_x/2,
ymin = data[i,3]-grob_length_y/2, ymax = data[i,3]+grob_length_y/2)}
Base
}
plot = testplot(data.frame(Function_results[,2]) )
plot
```