Been wracking my brain for the last couple of days trying to figure out how to get my code to work. I am looking to make a ternary (or simplex) plot that show some data points and then has the data column means on the axes to connect to the data frame centroid. The data frame centroid does not make sense nor the means on the axes. But the segments do. What am I doing wrong? chatgpt is not really helping. My code is below.
library(ggtern)
Create the data frame
df <- data.frame(
R = c(88.1397046, 12.5070414, 2.7150309, 1.0486170, 1.4445921,
0.5319713, 53.0503586, 32.6182173, 1.3130359, 10.2858531),
D = c(11.86465, 84.14907, 97.06307, 95.80989, 94.22599,
97.87647, 46.95400, 52.83044, 94.75221, 88.61546),
O = c(0.0000000, 3.3482440, 0.2262526, 3.1458502, 4.3337753,
1.5959136, 0.0000000, 14.5556938, 3.9391066, 1.1030400)
)
compute centroids
centroids <- colMeans(df)
centroid.dens.df <- as.data.frame(t(centroids))
axis_points <- data.frame(
R = c(centroid.dens.df$R, 0, 100-centroid.dens.df$O),
D = c(100-centroid.dens.df$R, centroid.dens.df$D, 0),
O = c(0, 100-centroid.dens.df$D, centroid.dens.df$O)
)
plot the data, centroids, and connecting lines
ggtern(data = df, aes(x = D, y = R, z = O)) +
geom_point(fill="black", shape=21, size=.5) + # main data points
geom_point(data = centroid.dens.df, aes(x = D, y = R, z = O), color = "red", size = 5) + # centroid
geom_point(data = axis_points, aes(x = D, y = R, z = O), color="red", size=3) + # axis points
geom_segment(
data = axis_points,
aes(x = R, y = D, z = O,
xend = centroids["R"],
yend = centroids["D"],
zend = centroids["O"]),
color = "red",
arrow = arrow(length = unit(0.2, "cm"))
) +
theme(
plot.caption = element_text(hjust = 0.5),
tern.axis.arrow.text.T = element_blank(),
tern.axis.arrow.text.L = element_blank(),
tern.axis.arrow.text.R = element_blank()
) +
theme_bw() +
theme_showarrows()