Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort and color pointranges in plot with facet grid and position dodge?

I have a facet gridded plot of two groups of fixed effects models. In group one I want to suppress the control variable (shown green). In group two I want to show the control variables if there is one in the model with a position_dodgev().

So far I figured out this code:

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

which gives me this:

enter image description here

However, in model 1 the explanatory variable is duplicated and the explanatory variable in the models of group 2 are not always on top.

I actually want to look the plot like this (photoshopped):

enter image description here

How would that be possible with ggplot()?


Data

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")
like image 800
jay.sf Avatar asked Jan 23 '26 18:01

jay.sf


1 Answers

A few small changes to your data will help fix the plot. First, we can remove the duplicated row for Model 1. Second, the issues with your cv colors changing order is that you have different control variables between Models 2a and 2b. We can create a indicator column to simply say whether or not the row is a control variable, and use that to color the plot.

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

like image 173
Jordo82 Avatar answered Jan 25 '26 08:01

Jordo82