Rebekah Scott
Rebekah Scott

Reputation: 35

"Ignoring unknown aesthetics: z and zend" but freaks out without z and zend

When using the ggtern library to plot data on the simplex, R keeps giving me the warning

In ggplot2::geom_segment(data = lines, ggplot2::aes(x = x1, y = x2,  :
  Ignoring unknown aesthetics: z and zend

If I remove z and zend, it will give an error

Error in FUN(X[[i]], ...) : object 'V3' not found.

Here's the code for reproducibility

my_matrix <- matrix(c(0.000000, 0.000000, 1.000000,
                      0.666666, 0.000001, 0.333333,
                      1.000000, 0.000000, 0.000000,
                      0.000001, 0.333333, 0.666667,
                      0.333334, 0.333335, 0.333331,
                      0.333332, 0.666668, 0.000000,
                      0.000000, 0.666666, 0.333334,
                      0.666667, 0.333333, 0.000000,
                      0.000000, 1.000000, 0.000000,
                      0.333333, 0.000001, 0.666665),
                    nrow = 10, ncol = 3, byrow = TRUE)

design <- as.data.frame(my_matrix)
lines <- data.frame(x1 = c(0.5, 0, 0.5),
                    x2 = c(0.5, 0.5, 0),
                    x3 = c(0, 0.5, 0.5),
                    xend = c(1, 1, 1)/3,
                    yend = c(1, 1, 1)/3,
                    zend = c(1, 1, 1)/3)
ggtern::ggtern(design, ggplot2::aes(x = V1, y = V2, z = V3)) +
  ggplot2::geom_point(size = 3, color = "blue") +
  ggplot2::geom_segment(data = lines, ggplot2::aes(x = x1, y = x2,
                                                   xend = xend, yend = yend),
                        color = "grey", size = 0.2) +
  ggplot2::theme_bw() + ggtern::theme_nomask() + ggtern::theme_clockwise()

I know the package::function() syntax is super annoying, I'm writing this for a plotting function in a toy R package, hence why there's no library(ggplot2) and library(ggtern).

So, any tips to get this warning to go away? What am I doing wrong? Does this have to do with ggtern being outdated?

I'm using ggplot2 version 3.4.1 and ggtern 3.4.1.

Upvotes: 3

Views: 281

Answers (1)

Jon Spring
Jon Spring

Reputation: 67000

It looks like ggtern overrides geoms' normal mapping and requires each layer to be expressed in x/y/z coordinates where they'd normally just need x/y. So geom_segment needs a z and a zend. Your global aes() in the ggtern line says that z will be found in the V3 column, but lines doesn't have that column, leading to the error. We need to specify where the z and zend will be found in the lines data.

ggtern::ggtern(design, ggplot2::aes(x = V1, y = V2, z = V3)) +
  ggplot2::geom_point(size = 3, color = "blue") +
  ggplot2::geom_segment(data = lines, ggplot2::aes(x = x1, y = x2, z = x3, 
                        xend = xend, yend = yend, zend = zend), 
                        color = "grey", size = 0.2) +
  ggplot2::theme_bw() + ggtern::theme_nomask() + ggtern::theme_clockwise()

enter image description here

Upvotes: 3

Related Questions