代码之家  ›  专栏  ›  技术社区  ›  Eric Green

向ggalt::geom\u dumbbell plot添加图例并对y轴进行排序

  •  2
  • Eric Green  · 技术社区  · 7 年前

    this SO answer ,user@Crops显示如何将图例添加到 ggalt::geom_dumbbell

    library(ggalt)
    
    df <- data.frame(trt=LETTERS[1:5], l=c(20, 40, 10, 30, 50), r=c(70, 50, 30, 60, 80))
    df2 = tidyr::gather(df, group, value, -trt)
    
    ggplot(df, aes(y = trt)) + 
         geom_point(data = df2, aes(x = value, color = group), size = 3) +
         geom_dumbbell(aes(x = l, xend = r), size=3, color="#e3e2e1", 
                       colour_x = "red", colour_xend = "blue",
                       dot_guide=TRUE, dot_guide_size=0.25) +
         theme_bw() +
         scale_color_manual(name = "", values = c("red", "blue") )
    

    enter image description here

    我要分类 trt 下降 r . 我试着替换 y = trt 具有 y = reorder(trt, r) R 找不到。

    1 回复  |  直到 6 年前
        1
  •  3
  •   markus    7 年前

    这里有一个方法,我们重新排序的因素水平 trt 在里面 df df2 在我们策划之前。

    # reorder factor levels
    df$trt <- reorder(df$trt, df$r)
    df2$trt <- factor(df2$trt, levels = levels(df$trt))
    
    ggplot(df, aes(y = trt)) + 
      geom_point(data = df2, aes(x = value, color = group), size = 3) +
      geom_dumbbell(aes(x = l, xend = r), size=3, color="#e3e2e1", 
                    colour_x = "red", colour_xend = "blue",
                    dot_guide=TRUE, dot_guide_size=0.25) +
      theme_bw() +
      scale_color_manual(name = "", values = c("red", "blue") )
    

    enter image description here

        2
  •  0
  •   Foo Cheung    5 年前

    ##Reformat data
    df3<-df  %>% arrange(r)
    
    df2<-df%>% mutate("key"="trt")
    df2$trt<-factor(df2$trt,df3$trt)
    
    ##plot
    dumbbell::dumbbell(df2, id="trt", column1="l", column2="r",key="key", delt =1, textsize=3, lab1 = "l", lab2="r", pt_val = 1, pointsize = 3,pt_alpha = 0.6, arrow=1, leg = "Add legend title", pval=2) + xlim(8,85) + facet_wrap(key ~.)
    

    我没有足够的点嵌入这里是链接。希望有人觉得有用。 dumbbell R Package

    推荐文章