7

我在 R 中使用 ggtern 制作三元图,并希望在我的 ggtern 图中使用轴标签和中断,与原始数据相同。对于下面代码中生成的数据,每个轴的最大值为 12、10 和 4。

在上一篇文章之后,我尝试使用中断和标签来执行此操作,但每个轴仍处于 0-1 比例,标签丢失(由于它们超过 1)并且带有标签的轴线不与点相交在情节上。(如何更改 ggtern 制作的三元图的标签?

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

mydata <- data.frame(
  x = runif(50, min = 0.25, max = 12),
  y = runif(50, min = 0.1, max = 10),
  z = runif(50, min = 0.5, max = 4),
  value = runif(50, min = 10000, max = 20000))

ggtern(data = mydata,aes(x = x, y = y, z = z,col=value)) + 
  theme_bw() +
  geom_point(alpha = 0.8, size = 3) +
  theme_showarrows() +
  scale_T_continuous(breaks=unique(mydata$x),labels=labFnc(mydata$x))+ 
  scale_L_continuous(breaks=unique(mydata$y),labels=labFnc(mydata$y))+ 
  scale_R_continuous(breaks=unique(mydata$z),labels=labFnc(mydata$z))

在此处输入图像描述

有没有办法做到这一点?任何帮助将不胜感激。

编辑:我也尝试添加 tern_limits 参数。虽然这看起来会按比例扩展绘图,但数据位于错误的位置。而且我不能像以前一样添加我独特的休息时间。

ggtern(data = mydata,aes(x = x, y = y, z = z,col=value)) + 
  theme_bw() +
  geom_point(alpha = 0.8, size = 3) +
  theme_showarrows() +
  tern_limits(T=12, L=10, R=4)

在此处输入图像描述

4

1 回答 1

7

您提供的解决方案是正确的,但是,limit_term(...)函数(或别名)的所有参数都应在对应于 [0,100%] 的 [0,1] 范围内。可以在此范围之外提供值,但是,这将用于解决包含大于 100% 和小于 0% 的值的限制。

总之,使用以下内容:

tern_limits(T=12, L=10, R=4)

实际上是要求三元限制分别受 1200%、1000% 和 400% 最大值的约束,这与您的尝试结果完全相同。

无论如何,这里有一些limits_ternzoom功能的例子。

library(ggtern)

n  = 100
df = data.frame(id=1:n,
                x=runif(n),
                y=runif(n),
                z=runif(n))
base = ggtern(df,aes(x,y,z,color=id)) + geom_point(size=3)
base

#Top Corner
base + limit_tern(1.0,0.5,0.5)

#Left Corner
base + limit_tern(0.5,1.0,0.5)

#Right Corner
base + limit_tern(0.5,0.5,1.0)

#Center Zoom Convenience Function
base + theme_zoom_center(0.4) # Zoom In
base + theme_zoom_center(0.6) # Zoom In
base + theme_zoom_center(0.8) # Zoom In
base + theme_zoom_center(1.0) ##Default as per no zoom
base + theme_zoom_center(1.2) # Zoom Out
base + theme_zoom_center(1.4) # Zoom Out
base + theme_zoom_center(1.6) # Zoom Out
base + theme_zoom_center(1.8) # Zoom Out
base + theme_zoom_center(2.0) # Zoom Out

#Left Zoom Convenience Function 
#   (try theme_zoom_R and theme_zoom_T for Right and Top respectively)
base + theme_zoom_L(0.4) # Zoom In
base + theme_zoom_L(0.6) # Zoom In
base + theme_zoom_L(0.8) # Zoom In
base + theme_zoom_L(1.0) ##Default as per no zoom
base + theme_zoom_L(1.2) # Zoom Out
base + theme_zoom_L(1.4) # Zoom Out
base + theme_zoom_L(1.6) # Zoom Out
base + theme_zoom_L(1.8) # Zoom Out
base + theme_zoom_L(2.0) # Zoom Out

注意:这些都是使缩放比通过 scale_X_continuous(...) [X = T,L,R] 独立控制限制(这是有效的)更容易的便利功能。与 x 和 y 独立的纯笛卡尔坐标系不同,在三元系统中,限制必须有意义,以便三个顶点满足单纯形的条件。

如果您必须独立控制每个轴,下面是一个示例,其中定义了每个限制、轴中断和轴标签,分别用于 T、L 和 R 轴。如果限制在单纯形条件方面是无意义的,则会引发错误。

ggtern() + 
  scale_T_continuous(limits=c(0.5,1.0),
                     breaks=seq(0,1,by=0.1),
                     labels=LETTERS[1:11]) + 
  scale_L_continuous(limits=c(0.0,0.5),
                     breaks=seq(0,1,by=0.1),
                     labels=LETTERS[1:11]) +
  scale_R_continuous(limits=c(0.0,0.5),
                     breaks=seq(0,1,by=0.1),
                     labels=LETTERS[1:11])
于 2018-04-11T02:41:25.673 回答