File: ternary.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (58 lines) | stat: -rw-r--r-- 1,475 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# An improvement on https://www.r-bloggers.com/ternary-plots-in-r-using-plotly/

library(plotly)

# acquire data
ds <- jsonlite::fromJSON(
  "https://gist.githubusercontent.com/davenquinn/988167471993bc2ece29/raw/f38d9cb3dd86e315e237fde5d65e185c39c931c2/data.json"
)
df <- dplyr::bind_rows(ds, .id = "id")

# reusable function for creating annotation object
label <- function(txt) {
  list(
    text = txt, 
    x = 0.1, y = 1,
    ax = 0, ay = 0,
    xref = "paper", yref = "paper", 
    align = "center",
    font = list(family = "serif", size = 15, color = "white"),
    bgcolor = "#b3b3b3", bordercolor = "black", borderwidth = 2
  )
}

# reusable function for axis formatting
axis <- function(txt) {
  list(
    title = txt, tickformat = ".0%", tickfont = list(size = 10)
  )
}

ternaryAxes <- list(
  aaxis = axis("Clay"), 
  baxis = axis("Sand"), 
  caxis = axis("Silt")
)

plot_ly(
  df, a = ~clay, b = ~sand, c = ~silt, color = I("black"), type = "scatterternary"
) %>%
  layout(
    annotations = label("Ternary Markers"), ternary = ternaryAxes
  )

plot_ly(
  df, a = ~clay, b = ~sand, c = ~silt, color = I("black"), type = "scatterternary", 
  split = ~id, mode = "lines"
) %>%
  layout(
    annotations = label("Ternary Lines"), ternary = ternaryAxes
  )

plot_ly(
  df, a = ~clay, b = ~sand, c = ~silt, color = ~id, type = "scatterternary",
  fill = "toself", mode = "lines"
) %>%
  layout(
    annotations = label("Ternary Contour"), ternary = ternaryAxes
  )