WeakLearner
WeakLearner

Reputation: 938

plotting two paths on ggtern plot in R

In the ggtern package in R, I am trying to plot two paths of different colors on the same ternery plot, and label their starting points ONLY, could someone show me how to do this, I can get the path on single plots, but not together on the same one, here is my example: require(ggtern)

require(ggtern)    
x  <- data.frame(
      A = c( 0, 0, 1, 0.1), 
      B = c( 0, 1, 0, 0.3) ,
      C = c( 1, 0, 0, 0.6)
    )

    yy<-data.frame(
      D= c(0.6, 0.2,0.8,0.33 ),
      E= c(0.2, 0.8, 0.1,0.33),
      F= c(0.2, 0.0, 0.1,0.33)   
        )

    ggtern(data=x,aes(A,B,C)) + 
      geom_path(color="red")+
      geom_point(type="l",shape=21,size=2) +
      geom_text(label="", color="blue")+
      theme_classic() 

    ggtern(data=yy,aes(D,E,F)) + 
      geom_path(color="blue")+
      geom_point(type="l",shape=21,size=1) +
      theme_classic() 

Upvotes: 2

Views: 539

Answers (1)

Nicholas Hamilton
Nicholas Hamilton

Reputation: 10506

Here I provide an answer to your question, also taking the opportunity to demonstrate some of the additional functionality of ggtern 2.0.1, which was published on CRAN a couple of days ago after completely re-writing the package to be compatible with ggplot2 2.0.0. A summary of the new functionality in ggtern 2.0.X can be found here:

Eric Fail is correct in saying that the best solution requires that the data to be combined into a single dataframe, and the paths either grouped or mapped to a different variable for colour, in order to distinguish between them. An alternate way is to create two(2) path layers, with a local dataframe passed to each geometry, rather than using the global dataframe passed to the ggtern constructor.

In the following solution, I have combined the data, created a 'Series' variable (subsequently mapped to colour), and then made use of the new geom_label(...) geometry that comes with the new version of ggplot2. Since some of the points lie on the perimeter (and the labels extend beyond the perimeter), I have also applied a manual clipping mask under the layers, which suppresses ggterns automatic clipping mask -- normally rendered in the foreground. I have also applied the theme_rotate(...) convenience function for the purposes of demonstration, and made use of the limit_tern(...) convenience function to extend the range of the axes beyond the standard range of [0,1]. Finally, new labels have been created for the procession arrows, which are different from the apex labels.

solution

The above solution can be produced with the following code:

require(ggtern)    
df.A <- data.frame(
  A = c( 0, 0, 1, 0.1), 
  B = c( 0, 1, 0, 0.3) ,
  C = c( 1, 0, 0, 0.6)
)

df.B <-data.frame(
  A= c(0.6, 0.2,0.8,0.33 ),
  B= c(0.2, 0.8, 0.1,0.33),
  C= c(0.2, 0.0, 0.1,0.33)   
)

df = rbind(data.frame(df.A,Series='A'),
           data.frame(df.B,Series='B'))
df$Label = 1:nrow(df)

ggtern(data=df,aes(A,B,C,colour=Series)) +
  theme_dark() + 
  theme_legend_position('topleft') +
  theme_showarrows() + custom_percent('%') + 
  theme_rotate(60) + 
  geom_mask() +
  geom_path(size=1) +
  geom_label(aes(label=Label),show.legend = F) + 
  limit_tern(1.1,1.1,1.1) + 
  labs(title  ="Example Combined Paths",
       Tarrow = "Value B",
       Larrow = "Value A",
       Rarrow = "Value C")

Upvotes: 2

Related Questions