如何在ggtern中显示数据的实际值(%)? [英] How can I show the real values (%) of my datas in ggtern?

查看:194
本文介绍了如何在ggtern中显示数据的实际值(%)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望获得与以下示例所示大致相同的结果.我在这两个功能之间犹豫不决:geom_crosshair_ternscale_X_continuous.

I would like to get approximately the same result as it is shown in the example below. I hesitate between these two functions to get this result: geom_crosshair_tern and scale_X_continuous.

北卡罗来纳州汉密尔顿,& Ferry,M.(2018年). ggtern:使用ggplot2的三元图.统计软件杂志,87(1),1-17. https://doi.org/10.18637/jss.v087.c03

这是我到目前为止所写的脚本:

Here is the script i have written until now:

points1 <- data.frame(
            rbind(c( 1,45,30,25),
                  c( 2,33,33,34),
                  c( 3,15,75,10)
            )
          )
colnames(points1) = c("IDPoint","X","Y","Z")

#geom_crosshair_tern version
base1 = ggtern(data=points1,aes(X,Y,Z)) +
  theme_bw() +
  tern_limits(labels=c(20,40,60,80,100), breaks=seq(0.2,1,by=0.2)) +
  theme_clockwise() +
  theme_showarrows() +
  labs(title  = "Test",Tarrow = "% X",Larrow = "% Y",Rarrow = "% Z") +
  theme(tern.axis.arrow=element_line(size=1,color="black")) +
  geom_point(shape=21,size=5,col="black",bg="slategray1") +
  geom_text(aes(label=IDPoint),color="black") +
  geom_crosshair_tern(lty=2)

这是geom_crosshair_tern的功能,但有两个问题:

Here is what I get with geom_crosshair_tern, but I have two issues:

  1. 我不知道如何显示数据的真实值;
  2. 我不知道如何将我的观点放在geom_crosshair_tern之上.
  1. I don't know how to show the real values of my datas;
  2. I don't know how to put my points above the geom_crosshair_tern.

scale_X_continuous功能更合适吗?我尝试以下脚本失败.

Is the scale_X_continuous function more appropriate? I tried unsuccessfully the following script.

points1 <- data.frame(
            rbind(c( 1,45,30,25),
                  c( 2,33,33,34),
                  c( 3,15,75,10)
            )
          )
colnames(points1) = c("IDPoint","X","Y","Z")

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

base1 = ggtern(data=points1,aes(X,Y,Z)) +
  scale_T_continuous(breaks=unique(points1$y),labels=labFnc(points1$y)+
  scale_L_continuous(breaks=unique(points1$x),labels=labFnc(points1$x)+
  scale_R_continuous(breaks=unique(points1$z),labels=labFnc(points1$z)+
  theme_bw() +
  tern_limits(labels=c(20,40,60,80,100), breaks=seq(0.2,1,by=0.2)) +
  theme_clockwise() +
  theme_showarrows() +
  labs(title  = "Test",Tarrow = "% X",Larrow = "% Y",Rarrow = "% Z") +
  theme(tern.axis.arrow=element_line(size=1,color="black")) +
  geom_point(shape=21,size=5,col="black",bg="slategray1") +
  geom_text(aes(label=IDPoint),color="black")

推荐答案

更正第二个将点放置在geom_crosshair_tern上方的问题,您需要通过从geom_crosshair_tern然后是点开始重新排列几何的顺序. 要在三角形外部打印,您需要在绘图调用中添加theme_nomask,然后就可以使用vjusthjust进行正确的标签放置.

Correct the second problem of placing the points above the geom_crosshair_tern, you needed to rearrange the order of the geometries by starting with geom_crosshair_tern and then the points. To print outside the triangle, you need to add theme_nomask to the plotting calls, then it is a matter of playing around with the vjust and hjust for the proper label placement.

library(ggtern)

points1 <- data.frame(
  rbind(c( 1,45,30,25),
        c( 2,33,33,34),
        c( 3,15,75,10)
  )
)
colnames(points1) = c("IDPoint","X","Y","Z")

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

#geom_crosshair_tern version


base2 = ggtern(data=points1,aes(X,Y,Z)) +
  theme_bw() +
  geom_crosshair_tern(lty=2)+
  tern_limits(labels=c(20,40,60,80,100), breaks=seq(0.2,1,by=0.2)) +
  theme_clockwise() +
  theme_showarrows() +
  theme_nomask() + 
  labs(title  = "Test",Tarrow = "% Y",Larrow = "% X",Rarrow = "% Z") +
  theme(tern.axis.arrow=element_line(size=1,color="black")) +
  geom_point(shape=21,size=5,col="black",bg="slategray1") +
  geom_text(aes(label=IDPoint), color="black") +
    annotate(geom  = 'text',
                x     = points1$X,
                y     = c(0),
                z     = 100-points1$X,
                vjust = c(-0., -0., -0.),
                hjust = c(-0.25, -0.25, -0.25),
                angle = c(-60,-60,-60),
                label = paste("X=",points1$X)) +
    annotate(geom  = 'text',
           x     = 100-points1$Y,
           y     = points1$Y,
           z     = c(0),
           vjust = c(+0.25, 0.25, 0.25),
           hjust = c(1, +1, 1),
           label = paste("Y=",points1$Y)) +
    annotate(geom  = 'text',
           x     = c(0), 
           y     = 100-points1$Z,
           z     = points1$Z,
           vjust = c(+0.3, 0.3, 0.3),
           hjust = c(-0.25, -0.25, -0.25),
           angle = c(60,60,60),
           label = paste("Z=",points1$Z))  

print(base2)

这篇关于如何在ggtern中显示数据的实际值(%)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆