代码之家  ›  专栏  ›  技术社区  ›  Yoann Pageaud

对于创建GGPLOT列表的循环,始终为点和线段保存相同的坐标

  •  0
  • Yoann Pageaud  · 技术社区  · 6 年前

    首先,您需要加载这些包:

    library(ggplot2)
    library(ggrepel)
    

    V1          V2           V3          V4          V5          V6          V7          Groups
    0.05579838 -0.44781204 -0.164612982 -0.05362210 -0.23103516 -0.04923499 -0.06634579      1
    0.14097924 -0.35582736  0.385218841  0.18004788 -0.18429327  0.29398646  0.69460669      2
    0.10699838 -0.38486299 -0.107284020  0.16468591  0.48678593 -0.70776085  0.20721932      3
    0.22720072 -0.30860464 -0.197930310 -0.24322096 -0.30969028 -0.04460600 -0.08420536      4
    0.24872635 -0.23415141  0.410406163  0.07072146 -0.09302970  0.01662256 -0.21683816      5
    0.24023670 -0.27807097 -0.096301697 -0.02373198  0.28474825  0.27397862 -0.29397324      6
    0.30358363  0.05630646 -0.115190308 -0.51532428 -0.08516130 -0.08785924  0.12178198      7
    0.28680386  0.07609196  0.488432908 -0.13082951  0.00439161 -0.17572986 -0.25854047      8
    0.30084361  0.06323714 -0.008347161 -0.26762137  0.40739524  0.22959024  0.19107494      9
    0.27955675  0.22533959 -0.095640072 -0.27988676 -0.04921808 -0.10662521  0.19934074     10
    0.25209125  0.22723231  0.408770841  0.13013867 -0.03850484 -0.23924023 -0.16744745     11
    0.29377963  0.13650247 -0.105720288 -0.00316556  0.29653723  0.25568169  0.06087608     12
    0.24561895  0.28729625 -0.167402464  0.24251060 -0.22199262 -0.17182828  0.16363196     13
    0.25150342  0.25298115 -0.147945172  0.43827820  0.02938933  0.01778563  0.15241257     14
    0.30902922 -0.01299330 -0.261085058  0.13509982 -0.40967529 -0.11366113 -0.06020937     15
    0.28696274 -0.12896680 -0.196764195  0.39259942  0.08362863  0.25464125 -0.29386260     16
    

    dframe <-
      rnorm(70) %>%
      matrix(nrow = 10) %>%
      as_tibble() %>%
      setNames(paste0("V", 1:ncol(.))) %>%
      mutate(Groups = 1:nrow(.)
             , Label = 1:nrow(.))
    

    我创建了一个表,其中包含我希望从数据框中使用的列组合:

    #Create all possible combinations
    combs<-expand.grid(seq(7),seq(7))
    #Remove duplicate and order
    combs<-combs[combs$Var1 != combs$Var2,]
    combs<-combs[order(combs[,1]),]
    

    list_EVplots<-list()
      for(i in seq(nrow(combs))){
        list_EVplots[[paste(combs[i,1],"&",combs[i,2])]]<- ggplot(data=dframe) +
          ggtitle(paste("Eigenvector Plot - Pairwise",
                        "correlation with","adjustment")) +
          geom_point(aes(x = dframe[,combs[i,1]], y = dframe[,combs[i,2]],
                         color = Groups)) +
          geom_segment(aes(x = rep(0,nrow(dframe)), y = rep(0,nrow(dframe)),
                           xend = dframe[,combs[i,1]], yend = dframe[,combs[i,2]],
                           color = Groups),
                       size = 1, arrow = arrow(length = unit(0.3,"cm"))) +
          geom_label_repel(aes(x = dframe[,combs[i,1]], y = dframe[,combs[i,2]],
                               label = rownames(dframe))) +
          scale_color_manual(values=colors) +
          xlab(paste0("Eigenvector ",combs[i,1])) +
          ylab(paste0("Eigenvector ",combs[i,2])) +
          theme(plot.title = element_text(hjust = 0.5),
                axis.title = element_text(size = 13),
                legend.text = element_text(size=12)) +
          geom_hline(yintercept = 0, linetype="dashed") +
          geom_vline(xintercept = 0, linetype="dashed")
      }
    

    在运行这个for循环之后,我获得了我的列表“list_EVplots”。 我认为坐标会锁定在第一次迭代的第一个绘图中使用的坐标上。 如果有人能解决这个问题,我将非常感谢你的帮助。

    使用R Studio在Linux 16.04下工作。R版本3.5.2(2018-12-20)-“蛋壳冰屋”

    预期:列表应包含不同的绘图:所有绘图应不同。

    问题:列表中的点和线段的所有绘图都具有相同的坐标。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Mark Peterson    6 年前

    最简单的答案往往是最简单的:尽量避免使用 for 循环在哪里 lapply 这更合适。我看不到您的代码中有任何明显的地方表明问题所在,但我猜这是深层嵌套中的一个问题 []

    下面是一个使用 aes_string 来处理变量。如果您想要的不是完整的成对绘图集,则可能需要修改对这两个绘图集的调用 拉普拉

    首先,一些可复制的数据(使用 dplyr ).注意,我做了 Labels ggplot ).

    dframe <-
      rnorm(70) %>%
      matrix(nrow = 10) %>%
      as_tibble() %>%
      setNames(paste0("V", 1:ncol(.))) %>%
      mutate(Groups = 1:nrow(.)
             , Label = 1:nrow(.))
    

    my_cols <-
      names(dframe)[1:7] %>%
      setNames(.,.)
    

    然后,只需设置一个嵌套的 拉普拉

    plot_list <-
      lapply(my_cols, function(col1){
        lapply(my_cols, function(col2){
    
          if(col1 == col2){
            return(NULL)
          }
    
          ggplot(dframe) +
            ggtitle(paste("Eigenvector Plot - Pairwise",
                          "correlation with","adjustment")) +
            geom_point(aes_string(x = col1
                                  , y = col2
                                  , color = "Groups")) +
            geom_segment(aes_string(xend = col1
                                    , yend = col2
                                    , color = "Groups")
                         , x = 0
                         , y = 0
                         , size = 1
                         , arrow = arrow(length = unit(0.3,"cm"))) +
            geom_label_repel(aes_string(x = col1
                                        , y = col2
                                        , label = "Label")) +
            xlab(paste0("Eigenvector ", col1)) +
            ylab(paste0("Eigenvector ", col2)) +
            theme(plot.title = element_text(hjust = 0.5),
                  axis.title = element_text(size = 13),
                  legend.text = element_text(size=12)) +
            geom_hline(yintercept = 0, linetype="dashed") +
            geom_vline(xintercept = 0, linetype="dashed")
    
        })
      })
    

    推荐文章