Created
April 12, 2017 20:33
-
-
Save mrecos/0bdb700261c98e1aa38042c208d92398 to your computer and use it in GitHub Desktop.
A bit of code for simulating site areas and environmental gradients to be plotted as two networks in both geographic and feature space. Supporting a graphic in this tweet: https://twitter.com/Md_Harris/status/851983574249209856
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("ggraph") | |
library("igraph") | |
library("ggplot2") | |
# create some simulated sites that contain various numbers of measurements on a regular grid | |
site_dat <- data.frame(size = rep(c("A","B","C","D"), times = c(3,4,8,5)), | |
x = c(25,26,26, | |
40,41,41,40, | |
15,16,17,17,16,15,15,16, | |
40,41,42,42,41), | |
y = c(28,28,29, | |
7,7,8,8, | |
10,10,10,11,11,11,12,12, | |
40,40,40,41,41), | |
stringsAsFactors = FALSE) | |
# scale/center the coordinates for plotting purposes | |
site_dat$x_c <- as.numeric(scale(site_dat$x)) | |
site_dat$y_c <- as.numeric(scale(site_dat$y)) | |
# Create simulated environmental gradients for elevation and distance to water | |
elev <- expand.grid(x = 0:50, y = 0:50) %>% | |
data.frame() %>% | |
mutate(elev = exp(-(x-40)^2 / (2*8^2)), | |
elev_c = as.numeric(scale(elev))) | |
h20 <- expand.grid(x = 0:50, y = 0:50) %>% | |
data.frame() %>% | |
mutate(h20 = (50-x)^2*(50-y)^2, | |
h20_c = as.numeric(scale(h20))) | |
var_space <- full_join(elev, h20, by = c("x", "y")) | |
# join the environmental measurements to the sites | |
site_dat <- site_dat %>% | |
left_join(var_space, by = c("x", "y")) | |
# create a new data.frame that gathers coords for geographic and feature space | |
# slighlty tortured here, but it was late when I wrote it! | |
site_dat2 <- data.frame(size = c(site_dat$size, paste0(site_dat$size, "_c")), | |
x_c = c(site_dat$x_c, site_dat$elev_c), | |
y_c = c(site_dat$y_c, site_dat$h20_c), | |
model = rep(c("geographic space", "feature space"), each = nrow(site_dat))) | |
# a data.frame of the network connections... I know 'df2' is a terrible name. | |
df2 <- data.frame(from = c(rep(c("A","B","C","D"), each = 4), | |
rep(c("A_c","B_c","C_c","D_c"), each = 4)), | |
to = c(rep(c("A","B","C","D"), times = 4), | |
rep(c("A_c","B_c","C_c","D_c"), times = 4)), | |
stringsAsFactors = FALSE) | |
# grab the coordinates (in both spaces) for the points | |
meta2 <- group_by(site_dat2, size) %>% | |
summarise(x = mean(x_c), y = mean(y_c)) %>% | |
mutate(label = substr(size,1,1)) | |
# use igraph functions to grab a unique edge list | |
df2 <- unique(get.data.frame(graph.data.frame(df2, directed=FALSE, | |
vertices = meta2),"edges")) | |
# run the graph on the unique edges with coordinates as vertices. | |
g2 <- graph.data.frame(df2, directed = FALSE, vertices = meta2) | |
# plot with ggraph package | |
ggraph(g2, layout = 'manual', node.positions = meta2) + | |
# plot edges with different colors | |
geom_edge_hive(aes(alpha = ..index.., color = rep(c("red", "blue"), each = 10)), | |
show.legend = FALSE) + | |
# plot the points for both sets of data | |
geom_point(data = site_dat2, aes(x_c, y_c, color = factor(model)), size = 2) + | |
# plot labels | |
geom_text(data = meta2, aes(x, y, label = label), size = 4, | |
nudge_x = 0.15, nudge_y = 0.15, family = "Trebuchet MS") + | |
# theme | |
theme_minimal() + | |
coord_fixed() + | |
# set scales | |
scale_x_continuous(breaks = seq(-1, 2, by = 0.5), labels = seq(-1, 2, by = 0.5)) + | |
scale_y_continuous(breaks = seq(-1, 2, by = 0.5), labels = seq(-1, 2, by = 0.5)) + | |
# Labels | |
labs(x = "X/Elevation", | |
y = "Y/H2O", | |
title = "Coordinate Space Transformation", | |
subtitle = "") + | |
# this hack removes the title of the legend | |
scale_color_discrete("") + | |
# theme elements for making it pretty | |
# If you do not have 'Trebuchet' font, you may want to remove those calls to 'family=...' | |
theme( | |
plot.title=element_text(family="TrebuchetMS-Bold", size = 11, hjust = 0.35), | |
legend.position = "bottom", | |
legend.text = element_text(family = "Trebuchet MS", size = 9), | |
panel.grid.major = element_blank(), | |
panel.grid.minor = element_blank(), | |
axis.text.x = element_text(angle = 90, hjust = 1, | |
size = 8, family = "Trebuchet MS"), | |
axis.text.y = element_text(size = 8, family = "Trebuchet MS"), | |
axis.title = element_text(size = 8, family = "Trebuchet MS", face = "bold") | |
) | |
ggsave("coord_transform.png", width = 4, height = 6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment