How to change labels of a ternary plot made by ggt

2019-06-02 09:06发布

问题:

I am trying to make ternary plot with ggtern. It looks very beautiful. But it only shows the percentage along the edges of the triangle, for example 100, 90 ... Is there a way to change that to show the real value of the underlining data? Thanks. I toke the example from this post. The original Post

The data looks like:

a <- c(0.1, 0.5,0.5, 0.6, 0.2, 0 , 0 , 0.004166667, 0.45) b <- c(0.75,0.5,0 , 0.1, 0.2, 0.951612903,0.918103448, 0.7875 , 0.45) c <- c(0.15,0 ,0.5, 0.3, 0.6, 0.048387097,0.081896552, 0.208333333, 0.10) d <- c(500,2324.90,2551.44,1244.50, 551.22,-644.20,-377.17,-100, 2493.04)

Here a, b, c are used as coordinates. What I want is to lable the breaks like 0.1, 0.2 (the cut values not the percentages like 10,20 ...).

回答1:

Someone wrote to me this week with a question, which is what I think you are after, perhaps this is what you mean?

library(ggtern)
set.seed(1)
df = data.frame(x=runif(10),y=runif(10),z=runif(10))

#Normalise sum to unity
df.s = rowSums(df); for(x in c('x','y','z')) df[,x] = df[,x]/df.s

labFnc <- function(x,digits=2) format(round(unique(x),digits),digits=digits)

#Plot
ggtern(df,aes(x,y,z)) +
  scale_T_continuous(breaks=unique(df$y),labels=labFnc(df$y)) +
  scale_L_continuous(breaks=unique(df$x),labels=labFnc(df$x)) +
  scale_R_continuous(breaks=unique(df$z),labels=labFnc(df$y)) +
  theme_bw() +
  theme_nogrid_minor() + 
  geom_point() +
  labs(title="Example Use of Data Along Ternary Axes")

This can be taken further, with the use of the geom_Xmark() (X = T,L,R) new geometries available with ggtern 2.1.0:

#Plot
ggtern(df,aes(x,y,z)) +
  scale_T_continuous(breaks=unique(df$y),labels=labFnc(df$y)) +
  scale_L_continuous(breaks=unique(df$x),labels=labFnc(df$x)) +
  scale_R_continuous(breaks=unique(df$z),labels=labFnc(df$y)) +
  theme_rgbw() +
  theme_nogrid() +  
  geom_Lmark(colour='darkblue') + 
  geom_Tmark(colour='darkred') +
  geom_Rmark(colour='darkgreen') + 
  geom_point() +
  labs(title="Example Use of Data Along Ternary Axes")



标签: r ggplot2