代码之家  ›  专栏  ›  技术社区  ›  jay.sf

如何用面网格和位置道奇对绘图中的点范围进行排序和着色?

  •  3
  • jay.sf  · 技术社区  · 7 年前

    我有两组固定效果模型的面网格图。在第一组中,我想抑制控制变量(显示为绿色)。在第二组中,如果模型中有一个控制变量 position_dodgev() .

    到目前为止,我发现了这个代码:

    library(ggplot2)
    library(ggstance)
    ggplot(mf, aes(fill=mn)) +
      geom_vline(xintercept=0) +
      geom_pointrangeh(aes(y=mn, x=coef, group=cv, color=cv, 
                           xmin=coef - se*1.96, xmax=coef + se*1.96),
                       position=position_dodgev(.5),
                       show.legend=FALSE) +
      scale_color_manual(values=c("green", "red", "green", "red")) +
      facet_grid(gr ~ ., scales="free", space="free")
    

    给了我这个:

    enter image description here

    然而,在模型1中,解释变量是重复的,并且组2模型中的解释变量并不总是在顶部。

    我真的想看看这个情节(photoshopped):

    enter image description here

    那怎么可能 ggplot() ?


    数据

    mf <- structure(list(coef = c(3.025, 0.762499999999999, -1.44237073106609, 
    -0.125042600081792, -0.689108910891089, 2.64264321029771, 2.64264321029771
    ), se = c(5.26319027539381, 3.34469904756018, 2.02098677878979, 
    2.02098677878979, 2.02098677878979, 0.763989041657158, 0.763989041657158
    ), mn = structure(c(1L, 1L, 2L, 2L, 3L, 4L, 4L), .Label = c("Model 2c", 
    "Model 2b", "Model 2a", "Model 1"), class = "factor"), gr = c(2, 
    2, 2, 2, 2, 1, 1), cv = structure(c(2L, 1L, 2L, 3L, 2L, 2L, 3L
    ), .Label = c("gear", "vs", "disp"), class = "factor")), row.names = c("vs", 
    "gear", "vs1", "disp", "1", "11", "2"), class = "data.frame")
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Jordo82    7 年前

    对数据进行一些小的更改将有助于修复绘图。首先,我们可以删除模型1的重复行。第二,你的简历颜色变化顺序的问题是你有不同的控制变量模型2a和2b。我们可以创建一个指标列,简单地说,行是否是一个控制变量,并使用它来给绘图上色。

    library(tidyverse)
    library(ggstance)
    
    mf %>% 
      #remove the dupe from Model 1
      filter(!(mn == "Model 1" & cv == "disp")) %>% 
      #create an indicator column for control variables
      mutate(Control = cv == "vs") %>% 
      ggplot(aes(fill=mn)) +
      geom_vline(xintercept=0) +
      #group and color using our new indicator variable
      geom_pointrangeh(aes(y=mn, x=coef, group=Control, color=Control, 
                           xmin=coef - se*1.96, xmax=coef + se*1.96),
                       position=position_dodgev(.5),
                       show.legend=FALSE) +
      scale_color_manual(values=c("green", "red", "green", "red")) +
      facet_grid(gr ~ ., scales="free", space="free")
    

    final plot