【问题标题】:How can I show the real values (%) of my datas in ggtern?如何在 ggtern 中显示我的数据的真实值 (%)?
【发布时间】:2019-06-08 02:14:07
【问题描述】:

我希望得到与以下示例中显示的大致相同的结果。我在这两个函数之间犹豫以获得这个结果:geom_crosshair_ternscale_X_continuous

Hamilton, N. E., & Ferry, M. (2018). ggtern: Ternary Diagrams Using ggplot2. Journal of Statistical Software, 87(1), 1‑17. https://doi.org/10.18637/jss.v087.c03

这是我到现在为止写的脚本:

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 得到的,但我有两个问题:

  1. 我不知道如何显示我的数据的真实值;
  2. 我不知道如何将我的观点放在geom_crosshair_tern 之上。

scale_X_continuous 函数更合适吗?我尝试了以下脚本,但没有成功。

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")

【问题讨论】:

    标签: r ggplot2 ggtern


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 关于您在三角形区域之外渲染的评论,您需要明确定位蒙版,然后在蒙版层之后添加图层。查看帮助文件中的?geom_mask
    【解决方案2】:

    这是另一种方法:

    #Duplicate Original Data Frame
    df = points1
    
    #New Data frame
    #NEW Variable to be used in text label
    df$label = apply(df,1,function(row){
      sprintf("[%s,%s,%s]",row[2],row[3],row[4])
    })
    
    #NEW Color Variable to be mapped
    df$color = as.factor(df$IDPoint)
    
    #NEW Fill Variable to be mapped
    df$fill  = as.factor(df$IDPoint)
    
    #geom_crosshair_tern version
    base2 = ggtern(data=df,aes(X,Y,Z,color=color,fill=fill)) + ## << color and fill global mappings
      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_crosshair_tern(lty=2) +  ## <<< Order brought forward, so that the crosshair is under the points
    
      geom_point(shape=21,size=5,color='black') + ## << Black border on points
      geom_text(aes(label=IDPoint),color="black") +
      geom_text(aes(label=IDPoint),color="black") +
    
      geom_text(aes(label=sprintf("   %s",label)),size=3,hjust=0) + ## << NEW Text Geometry
    
      guides(fill='none',color='none') ## << Turn off legends for colour/fill
    
    print(base2)
    

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多