R - ggplot2 - 可视化与基本模型的偏差 [英] R - ggplot2 - Visualize deviations from base model
问题描述
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)
我有来自基线模型和其他 5 个探索性模型的以下参数.我正在努力为读者做尽可能多的工作,所以我正在考虑超越这一点.
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.
有没有办法在 ggplot 中以显示与基线模型估计值的偏差的方式绘制它?我想不出任何值,因为有 6 个值.
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.
谢谢!
推荐答案
不确定这是否是您要找的.在这里我计算了每个模型之间的差异 &使用 purrr::map_df
的 base
模型.之后,我将结果转换为长格式以使用 ggplot2
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()
由 reprex 包 (v0.2.0) 于 2018 年 5 月 6 日创建.
Created on 2018-05-06 by the reprex package (v0.2.0).
这篇关于R - ggplot2 - 可视化与基本模型的偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!