使用facet_wrap在ggplot中绘制具有全范围的分位数回归 [英] Plotting Quantile regression with full range in ggplot using facet_wrap

查看:170
本文介绍了使用facet_wrap在ggplot中绘制具有全范围的分位数回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想在使用facet_wrap时以全范围绘制整个全范围分位数线.代码如下:

So I would like to plot entire full range quantile lines in full range when using facet_wrap. The code goes as follows:

library(tidyverse)
library(quantreg)

mtcars %>% 
  gather("variable", "value", -c(3, 10)) %>% 
  ggplot(aes(value, disp)) + 
  geom_point(aes(color = factor(gear))) + 
  geom_quantile(quantiles = 0.5, 
                aes(group = factor(gear), color = factor(gear))) +
  facet_wrap(~variable, scales = "free")
#> [multiple warnings removed for clarity]

reprex软件包(v0.3.0)于2019年12月5日创建 sup>

Created on 2019-12-05 by the reprex package (v0.3.0)

可以看出,回归线没有完整范围,我无法轻松解决.

As can be seen regression lines don't have full range and I cannot solve this easily.

推荐答案

这感觉是过度设计的,但是一种方法是将斜率截距图获取到ggplot之外,然后使用geom_abline进行绘制.此实现的潜在缺点是,它使用一些抖动来防止rq中的奇异设计矩阵"错误,但这意味着即使对于只有一个x值的数据,它也会生成随机斜率.为了解决这个问题,如果在变量计算组合中只有一个值,则可以从斜率计算中删除数据.

This feels over-engineered, but one approach would be to get the slope-intercept figures outside of ggplot and then plot them using geom_abline. A potential downside of this implementation is that it uses some jittering to prevent a "singular design matrix" error in rq, but this means that it would generate random slopes even for data with only one x value. To get around that, there's a step here to remove data from the slop calculation if it only has one value for that variable-gear combination.

mtcars %>% 
  gather("variable", "value", -c(3, 10)) -> mt_tidy

mt_tidy %>%
  # EDIT: Added section to remove data that only has one value for that
  #   variable and gear. 
  group_by(variable, gear) %>%
    mutate(distinct_values = n_distinct(value)) %>% 
    ungroup() %>%
    filter(distinct_values > 1) %>%
    select(-distinct_values) %>%

  nest_legacy(-c(variable, gear)) %>% 
  # the jittering here avoids the "Singular design matrix" error
  mutate(qtile = map(data, ~ rq(jitter(.x$disp) ~ jitter(.x$value), 
                                tau = 0.5)),
         tidied = map(qtile, broom::tidy)) %>%
  unnest_legacy(tidied) %>%
  select(gear:estimate) %>%
  pivot_wider(names_from = term, values_from = estimate) %>%
  select(gear, variable, 
         intercept = `(Intercept)`, 
         slope = `jitter(.x$value)`) -> qtl_lines

ggplot(mt_tidy, aes(value, disp, color = factor(gear))) + 
  geom_point() + 
  geom_abline(data = qtl_lines,
              aes(intercept = intercept, slope = slope,
                  color = factor(gear))) +
  facet_wrap(~variable, scales = "free")

这篇关于使用facet_wrap在ggplot中绘制具有全范围的分位数回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆