R - ggplot2 - Visualize deviations from base model

2019-08-02 22:22发布

base = c(1.84,3.92,1.67,1.12,1.63,.62,.59)
e1 = c(.61,1.47,1.68,1.95,1.64,.61,.72)
e2 = c(.64,7.08,1.67,1.12,1.44,.46,.76)
e3 = c(.64,4.47,1.68,2.04,1.45,.4,1.35)
e4 = c(.78,1.61,1.62,1.09,1.46,.66,.76)
e5 = c(.78,.99,1.62,2.32,1.46,.73,.52)

df = data.frame(base,e1,e2,e3,e4,e5)

I have the following parameters from a baseline model and 5 other exploratory models. I'm trying to do as much job as possible for the reader so I'm thinking about going beyond tabling this out.

Is there a way to plot this in ggplot in a way that would show deviations from the estimates of the baseline model? I can't think of any as there are 6 values.

Thanks!

1条回答
再贱就再见
2楼-- · 2019-08-02 22:50

Not sure if this is what you're looking for. Here I calculate the difference between each model & the base model using purrr::map_df. After that I convert the result to long format for plotting w/ ggplot2

library(tidyverse)

base = c(1.84,3.92,1.67,1.12,1.63,.62,.59)
e1 = c(.61,1.47,1.68,1.95,1.64,.61,.72)
e2 = c(.64,7.08,1.67,1.12,1.44,.46,.76)
e3 = c(.64,4.47,1.68,2.04,1.45,.4,1.35)
e4 = c(.78,1.61,1.62,1.09,1.46,.66,.76)
e5 = c(.78,.99,1.62,2.32,1.46,.73,.52)

df = data.frame(base, e1, e2, e3, e4, e5)

# calculate colwise differences
df %>% 
  map_df( ~ (. - base)) %>% 
  select(-base) %>% 
  # create id for each number
  mutate(id = row_number()) %>% 
  # convert to long format
  gather(key = "model", value = "diff", -id) -> df_dif

# plot the differences
ggplot(df_dif, aes(x = id, y = diff)) +
  geom_col(aes(fill = model), position = "dodge") +
  facet_grid(~ model) +
  theme_classic()

Created on 2018-05-06 by the reprex package (v0.2.0).

查看更多
登录 后发表回答