Created
April 26, 2018 13:56
-
-
Save ikashnitsky/2028fa2b08bb4e8cf3adbf71fd8ddcd5 to your computer and use it in GitHub Desktop.
a minimal example of an RGB colorcoded ternary diagram
This file contains hidden or 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
# a function from: Schöley, J., & Willekens, F. (2017). Visualizing compositional data on the Lexis surface. Demographic Research, 36(21), 627–658. https://doi.org/10.4054/DemRes.2017.36.21 | |
TernaryCentroidCoord <- function (k) { | |
centroids <- matrix(nrow = k^2, ncol = 5, | |
dimnames = list(NULL, c("j", "i", "p1", "p2", "p3"))) | |
for (j in 1:k) { | |
for (i in 1:(2*k - 2*j + 1)) { | |
p1 = (6*k - 6*j - 3*i + 4 + i%%2) / (6*k) | |
p2 = (6*j - 2 - 2*i%%2) / (6*k) | |
p3 = (3*i - 2 + i%%2) / (6*k) | |
centroids[i+(j-1)*(2*k-j+1),] = c(j, i, p1, p2, p3) | |
} | |
} | |
return(centroids) | |
} | |
df_leg <- TernaryCentroidCoord(100) %>% | |
as_tibble() %>% | |
mutate(color = rgb(p1, p2, p3)) | |
df_leg %>% | |
ggplot() + | |
geom_point(aes(p1, p2, p3, | |
group = 1, | |
color = color), | |
size = 3) + | |
# scale | |
scale_color_identity()+ | |
scale_L_continuous(breaks = seq(0, 1, .2)) + | |
scale_T_continuous(breaks = seq(0, 1, .2)) + | |
scale_R_continuous(breaks = seq(0, 1, .2)) + | |
# coord | |
coord_tern() + | |
# theme | |
theme()+ | |
theme_classic() %+replace% | |
theme(legend.position = "none", | |
plot.background = element_rect(fill = "transparent", colour = NA) | |
) | |
gg_tern_legend <- last_plot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment