[R] ggplot ternary plot: two quirks

Szumiloski, John John.Szumiloski at bms.com
Tue Jul 28 18:58:19 CEST 2015


Dear useRs,

I am using the ggtern package to generate ternary plots in ggplot2.  I am making a diagram demonstrating how to interpret a ternary plot and am using ggtern::ggtern.multi to generate it.  I intend to have three panels, one for the scale and grid for each component.  However, is not working.  Here it is:


require(ggplot2)
require( ggtern)

local({

d <- data.frame(X=0, Y=0, Z=1)  # dummy data
brks <- seq(0,5)/5              # axis breaks/ticks
minbrk <- seq(0.1,0.9,0.2)      # minor axis breaks


p1 <- ggplot() + theme_bw(base_size=16) + theme_noarrows() +
      scale_T_continuous(name="Cpnt 1", breaks=brks, minor_breaks=minbrk, labels=brks) +
      scale_R_continuous(name=      "", breaks=   0, minor_breaks=     0, labels="") +
      scale_L_continuous(name=      "", breaks=   0, minor_breaks=     0, labels="") +
      coord_tern() + geom_point(data=d, aes(X, Y, Z), size=0)

p2 <- ggplot() + theme_bw(base_size=16) + theme_noarrows() +
      scale_T_continuous(name=      "", breaks=   0, minor_breaks=     0, labels="") +
      scale_R_continuous(name=      "", breaks=   0, minor_breaks=     0, labels="") +
      scale_L_continuous(name="Cpnt 2", breaks=brks, minor_breaks=minbrk, labels=brks) +
      coord_tern() + geom_point(data=d, aes(X, Y, Z), size=0)

p3 <- ggplot() + theme_bw(base_size=16) + theme_noarrows() +
      scale_T_continuous(name=      "", breaks=   0, minor_breaks=     0, labels="") +
      scale_R_continuous(name="Cpnt 3", breaks=brks, minor_breaks=minbrk, labels=brks) +
      scale_L_continuous(name=      "", breaks=   0, minor_breaks=     0, labels="") +
      coord_tern() + geom_point(data=d, aes(X, Y, Z), size=0)

ggtern.multi(p1, p2, p3, cols=3)

}) # end local

This code does generate three panels, but only the first is rendered as I intended.  The latter two have NAs as the axis/scale labels, and the whole code throws the warning:

Warning messages:
1: In `[<-.factor`(`*tmp*`, ri, value = c(0, 0.2, 0.4, 0.6, 0.8, 1)) :
        invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ri, value = c(0, 0.2, 0.4, 0.6, 0.8, 1)) :
  invalid factor level, NA generated

In addition, if I put the dummy data and the scale breaks objects in the global environment and run each of the three component plots separately, only p1 works.  p2 and p3 fail in the same way so the combining is not likely the culprit.


----------------------------

I have discovered a feature of the scale_<TLR>_continuous call which may be related.  When I am making truncated ternary plots (ranges a subset of [0, 1]), I can successfully customize axes in the following way:


  ggplot() + theme_bw(base_size=18) +
      scale_T_continuous(name="Cmpt 1", breaks=seq(0.6,0.7,0.02), minor_breaks=seq(0.61,0.71,0.01),
                         labels=seq(0.6,0.7,0.02)) +
      scale_R_continuous(name="Cmpt 2", breaks=seq(0.1,0.3,0.02), minor_breaks=seq(0.11,0.31,0.01),
                         labels=seq(0.1,0.3,0.02)) +
      scale_L_continuous(name="Cmpt  3", breaks=seq(0.1,0.3,0.02), minor_breaks=seq(0.11,0.31,0.01),
                         labels=seq(0.1,0.3,0.02)) +
      coord_tern(Tlim=c(0.595,0.690), Llim=c(0.160,0.255), Rlim=c(0.150,0.245)) +
      geom_point(data=data.frame(X=1/6, Y=2/3, Z=1/6), aes(X, Y, Z))

Note I have duplicated the breaks argument for the labels argument, to make the labels be raw fractions, not percent.  I wish to unify the formatting; this plots the endpoints only to one decimal place and I wanted two consistently.  So I try wrapping format() around only the labels argument of the scale_T_continuous layer:

  ggplot() + theme_bw(base_size=18) +
      scale_T_continuous(name="Cmpt 1", breaks=seq(0.6,0.7,0.02), minor_breaks=seq(0.61,0.71,0.01),

                         labels=format(seq(0.6,0.7,0.02), nsmall=2)) +  #####   <-----  here

      scale_R_continuous(name="Cmpt 2", breaks=seq(0.1,0.3,0.02), minor_breaks=seq(0.11,0.31,0.01),
                         labels=seq(0.1,0.3,0.02)) +
      scale_L_continuous(name="Cmpt 3", breaks=seq(0.1,0.3,0.02), minor_breaks=seq(0.11,0.31,0.01),
                         labels=seq(0.1,0.3,0.02)) +
      coord_tern(Tlim=c(0.595,0.690), Llim=c(0.160,0.255), Rlim=c(0.150,0.245)) +
      geom_point(data=data.frame(X=1/6, Y=2/3, Z=1/6), aes(X, Y, Z))

This leads to the same (double) warning as above, and the R and L axes/scales have only NAs for labels.  Interestingly the T axis labels are formatted as intended.

Any help or direction would be appreciated.

Thanks, John
John Szumiloski, Ph.D.
Principal Scientist, Statistician
Analytical and Bioanalytical Development
NBR105-1-1411

Bristol-Myers Squibb
P.O. Box 191
1 Squibb Drive
New Brunswick, NJ
08903-0191

(732) 227-7167


________________________________
 This message (including any attachments) may contain co...{{dropped:8}}



More information about the R-help mailing list