Home > Back-end >  Plotting ternary graph with imported excel sheet
Plotting ternary graph with imported excel sheet

Time:06-28

I have a number of excel sheets (example attached) which plot simple ternary plots.[![basic excel sheet layout][![1]][1]][1]

This currently is used to plot a simple ternary plot of shape [![generated simple ternary plot in excel][![2]][2]][2]

left scale is c:a ratio, right is b:a ratio at base is (a-b)/(a-c) ratio

What I would LIKE to do is to be able to plot the shape of particles with their average size and lithology colour coded using ggtern.

I am NOT having a lot of fun with this to say the least

> library("readxl")
library("ggtern")

#data <- read_excel("my_file.xlsx")
Tdata <- read_excel("C:\\Users\\sedeal\\Desktop\\R_studio\\1770.xlsx", range = "Data entry!a1:F54", na = "")


Tdata = Tdata[-1,]
Tdata = Tdata[-1,]
Tdata = Tdata[-1,]
Tdata = Tdata[,-1]

colnames(Tdata)
names(Tdata)[3] <- "a.axis"
names(Tdata)[4] <- "b.axis"
names(Tdata)[5] <- "c.axis"
names(Tdata)[5] <- "lithology"
names(Tdata)[5] <- "roundness"

View(Tdata)

"seems" to get the data into R studio.. [![R data table][5]][5][![enter image description here][6]][6]

PROBLEM 1: sapply(Tdata, typeof)

a.axis b.axis c.axis lithology roundness "character" "character" "character" "character" "character"

I can't work out how to change a.axis b-axis c.axis to numeric values

PROBLEM 2: Do the "NA" fields mess things up? They seem to in scatterplots

Warning message: Removed 34 rows containing missing values (geom_point).

PROBLEM 3: I need to work out the ratios c:a; b:a; (a-b)/(a-c) and then plot these in such a way that I have a chance to mess with the formatting.

PROBLEM 4: The biggest problem - I'm really pants at R-studio (so please accept apologies for what are probably really simple issues)

TABULATED DATA in the hope that it is helpful??

a-axis b-axis c-axis lithology roundness
11.11 10.98 5.92 a-ig sa
8.57 8.27 6.2 met sa
9.25 6.25 4.14 ss r
8.21 7.23 5.03 b-ig sa
8.34 6.18 3.28 a-ig r
7.36 5.69 4.43 ss sr
7.29 7.19 2.93 ss a
7.25 5.29 4.89 a-ig sr
7.95 5.16 4.46 met sa
6.03 4.76 4.57 a-ig sa
6.65 5.24 4.54 qtz va
6.12 4.58 4.29 b-ig r
7.43 4.11 3.29 b-ig a
4.92 3.41 3.17 ss sa
6.39 4.64 2.52 a-ig a
5.12 4.99 3.31 b-ig a

EDIT:

> dput(Tdata)```
structure(list(a.axis = c("11.11", "8.57", "9.25", "8.2100000000000009", 
"8.34", "7.36", "7.29", "7.25", "7.95", "6.03", "6.65", "6.12", 
"7.43", "4.92", "6.39", "5.12", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), b.axis = c("10.98", 
"8.27", "6.25", "7.23", "6.18", "5.69", "7.19", "5.29", "5.16", 
"4.76", "5.24", "4.58", "4.1100000000000003", "3.41", "4.6399999999999997", 
"4.99", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA), c.axis = c("5.92", "6.2", "4.1399999999999997", 
"5.03", "3.28", "4.43", "2.93", "4.8899999999999997", "4.46", 
"4.57", "4.54", "4.29", "3.29", "3.17", "2.52", "3.31", NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), lithology = c("a-ig", "met", "ss", "b-ig", "a-ig", "ss", "ss", 
"a-ig", "met", "a-ig", "qtz", "b-ig", "b-ig", "ss", "a-ig", "b-ig", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA), roundness = c("sa", "sa", "Roundness", "sa", "Roundness", 
"sr", "a", "sr", "sa", "sa", "va", "Roundness", "a", "sa", "a", 
"a", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA)), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))


The solution I'm working with (thanks to @SamR)

Tdata <- read.table(text = "a-axis  b-axis  c-axis  lithology   roundness
#COPY/PASTE EXCEL DATA INTO THIS SECTION

", header = T)

# Make numeric and calculate ratios

library(dplyr)
library(ggtern)


Tdata <- Tdata |>
    mutate(
        across(a.axis:c.axis, as.numeric),
        c_to_a = c.axis / a.axis,
        b_to_a = b.axis / a.axis,
        ab_ac = (a.axis - b.axis) / (a.axis - c.axis)
    )

#draw the plot


ggtern(data = Tdata, aes(x = c_to_a, y = ab_ac, z = b_to_a))  
    geom_point()  
    xlab("c:a")   # replace default axis labels
    ylab("b:a")  
    zlab("(a-b)/(a-c)")  
    theme_bw()  
    theme_showarrows()  
    theme_anticlockwise()


  [1]: https://i.stack.imgur.com/mkFjy.jpg
  [2]: https://i.stack.imgur.com/rBobu.jpg
  [3]: https://i.stack.imgur.com/lvAmQ.jpg
  [4]: https://i.stack.imgur.com/hYFdJ.jpg
  [5]: https://i.stack.imgur.com/eornO.jpg
  [6]: https://i.stack.imgur.com/Rh6du.jpg

CodePudding user response:

Using the data you posted in the table.

# Read in the data
Tdata <- read.table(text = "a-axis  b-axis  c-axis  lithology   roundness
11.11   10.98   5.92    a-ig    sa
8.57    8.27    6.2 met sa
9.25    6.25    4.14    ss  r
8.21    7.23    5.03    b-ig    sa
8.34    6.18    3.28    a-ig    r
7.36    5.69    4.43    ss  sr
7.29    7.19    2.93    ss  a
7.25    5.29    4.89    a-ig    sr
7.95    5.16    4.46    met sa
6.03    4.76    4.57    a-ig    sa
6.65    5.24    4.54    qtz va
6.12    4.58    4.29    b-ig    r
7.43    4.11    3.29    b-ig    a
4.92    3.41    3.17    ss  sa
6.39    4.64    2.52    a-ig    a
5.12    4.99    3.31    b-ig    a", header = T)

# Make numeric and calculate ratios

library(dplyr)
library(ggtern)


Tdata <- Tdata |>
    mutate(
        across(a.axis:c.axis, as.numeric),
        c_to_a = c.axis / a.axis,
        b_to_a = b.axis / a.axis,
        ab_ac = (a.axis - b.axis) / (a.axis - c.axis)
    )

Then it's just a question of drawing the plot:


ggtern(data = Tdata, aes(x = c_to_a, y = ab_ac, z = b_to_a))  
    geom_point()  
    xlab("c:a")   # replace default axis labels
    ylab("b:a")  
    zlab("(a-b)/(a-c)")  
    theme_bw()  
    theme_showarrows()  
    theme_anticlockwise()

enter image description here

EDIT: Added arrows to plot axes and switch the ratios (initially had a:c rather than c:a). I realise the plot does not look like the one in the question - I am not exactly sure why as your original one does not have axis labels so it is hard to see what the differences are.

CodePudding user response:

thanks to SamR

#setup
install.packages("ggplot2")
install.packages("ggtern")

library("ggtern")


# Read in the data
# COPY AND PASTE THE DATA IN below the headings
Tdata <- read.table(text = "a-axis  b-axis  c-axis  lithology   roundness
11.11   10.98   5.92    a-ig    sa
8.57    8.27    6.2 met sa
9.25    6.25    4.14    ss  r
8.21    7.23    5.03    b-ig    sa
8.34    6.18    3.28    a-ig    r
7.36    5.69    4.43    ss  sr
7.29    7.19    2.93    ss  a
7.25    5.29    4.89    a-ig    sr
7.95    5.16    4.46    met sa
6.03    4.76    4.57    a-ig    sa
6.65    5.24    4.54    qtz va
6.12    4.58    4.29    b-ig    r
7.43    4.11    3.29    b-ig    a
4.92    3.41    3.17    ss  sa
6.39    4.64    2.52    a-ig    a
5.12    4.99    3.31    b-ig    a


", header = T)

# Make numeric and calculate ratios

library(dplyr)
library(ggtern)


Tdata <- Tdata |>
    mutate(
        across(a.axis:c.axis, as.numeric),
        c_to_a = c.axis / a.axis,
        b_to_a = b.axis / a.axis,
        ab_ac = (a.axis - b.axis) / (a.axis - c.axis)
    )

#draw the plot


ggtern(data = Tdata, aes(x = c_to_a, y = ab_ac, z = b_to_a))  
    geom_point(aes( shape=lithology))  
    xlab("c:a")   # replace default axis labels
    ylab("b:a")  
    zlab("(a-b)/(a-c)")  
    theme_bw()  
    theme_showarrows()  
    theme_anticlockwise()

lithology shape plot

  • Related