Skip to content

Instantly share code, notes, and snippets.

@Rekyt
Last active August 29, 2015 14:13
Show Gist options
  • Save Rekyt/dcb714dd4afa518e01cb to your computer and use it in GitHub Desktop.
Save Rekyt/dcb714dd4afa518e01cb to your computer and use it in GitHub Desktop.
Rapid false gaussian plot with ggplot2
# Fast plot for gaussian function
#
library(ggplot2)
library(grid)
library(dplyr)
# Choose a window
seq = seq(-5, 5, 0.01)
# Compute density for Gaussian function
# Mean = 0, sd = 1
dens = dnorm(seq)
norm.dens = dens / max(dens) # Normalized density
# Compute points of presence absence
int = seq(-5, 5, 0.5)
# If between -2 and 2 puts a 1 else a 0
pres.abs = mutate(as.data.frame(int), pres = ifelse(int > -2, ifelse(int < 2, 1, 0), 0))
ggplot(data.frame(seq, norm.dens), aes(x = seq, y = norm.dens)) +
geom_line(size = 1.5, colour = "blue") +
geom_point(data = pres.abs, size = 4, aes(x = int, y = pres)) +
scale_y_continuous(breaks = c(0, 1)) +
theme_classic() +
labs(x = "Niche Axis (Ecological Variable)",
y = "Species Presence or Absence") +
annotate(geom = "segment", x = 0, xend = 0,
y = 0.25, yend = 0.95, size = 2,
arrow = arrow(length = unit(0.5,"cm")),
color = "blue") +
annotate("text", x = 0, y = 0.15, label = "Optimal Value\n=\nIndicator Value", lineheight = 0.6, color = "blue") +
theme(axis.text = element_text(size = rel(1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment