【问题标题】:ggplot2: geom_quantile looks odd and returns an unexpected errorggplot2:geom_quantile 看起来很奇怪并返回意外错误
【发布时间】:2020-06-25 08:14:46
【问题描述】:

请在下面找到我的数据样本p

问题:

(1) 下面生成的geom_quantile()-plot 看起来很奇怪,因为我不希望回归线在(8,0)-ish 处截取。它似乎排除了p$n.sygdom>8-values。为什么?

(2) 它返回以下错误 - 为什么会这样,这是什么意思?:

1: Removed 1 rows containing non-finite values (stat_quantile)

2: I rq.fit.br(wx, wy, tau = tau, ...) : Solution may be nonunique

我制作了这个情节:

因为我想绘制第 50 个百分位数,所以我指定了 .. geom_quantile(quantiles = 0.5, ...,如下面的脚本所示。我希望geom_quantile(quantiles=0.5) 拦截位于(10, 10)-ish 的geom_point 周围,或者至少在(10,0) 附近。 p$n.sygdom (Metastatic Burden)-values 在 9 和 10 似乎根本不包括在内。

我在运行此代码时收到上述错误。我怀疑这与图形上奇怪的geom_quantile 有关,但我不知道是什么。

cols = c("#E1B930", "#2C77BF","#E38072","#6DBCC3")

ggplot(p, aes(x=n.sygdom,y=os.neck)) + geom_point(aes(color=uiccc),shape=20, size=7,alpha=0.7) + 

  geom_quantile(quantiles = 0.5,col="black", size=1,linetype=2) + 


  scale_fill_manual(values=cols) + 

  scale_colour_manual(values=cols) + 

  scale_x_continuous(breaks = seq(0,18, by=1), name="Metastatic Burden") +
  scale_y_continuous(breaks = seq(0,200, by=25), limits=c(0,130), name="Time to death (months)") +

  theme(legend.position="none")

我的数据p

p <- structure(list(n.sygdom = c(4L, 1L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 
0L, 0L, 0L, 4L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
3L, 0L, 0L, 0L, 3L, 5L, 0L, 1L, 1L, 0L, 1L, 0L, 3L, 0L, 1L, 1L, 
5L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 1L, 2L, 0L, 1L, 0L, 
0L, 0L, 0L, 1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 2L, 10L, 6L, 0L, 
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
2L, 0L, 0L, 0L, 0L, 1L, 1L, 2L, 0L, 4L, 0L, 0L, 1L, 0L, 1L, 1L, 
1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
2L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 7L, 4L, 
0L, 2L, 1L, 0L, 4L, 0L, 2L, 7L, 4L, 6L, 2L, 0L, 0L, 1L, 1L, 0L, 
2L, 1L, 2L, 1L, 1L, 0L, 2L, 3L, 2L, 3L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 0L, 0L, 0L, 0L, 1L, 2L, 1L, 1L, 1L, 0L, 0L, 0L, 2L, 0L, 1L, 
0L, 2L, 2L, 0L, 0L, 0L, 0L, 9L, 0L, 2L, 6L, 0L, 9L, 0L, 1L, 0L, 
7L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 5L, 2L, 4L, 6L, 0L, 1L, 
4L, 0L, 0L, 1L, 1L, 2L, 1L), os.neck = c(11.5, 74.38, 17.02, 
7.89, 96.03, 40.48, 17.74, 14.65, 62.46, 12.55, 9.92, 26.05, 
45.47, 17.38, 39.72, 51.45, 8.61, 76.98, 67.09, 94.79, 72.15, 
93.93, 17.05, 12.48, 91.6, 15.87, 11.04, 67.22, 67.02, 8.94, 
6.6, 5.09, 10.68, 17.15, 0.07, 5.19, 40.77, 0.2, 170.88, 5.55, 
1.61, 38.28, 10.58, 32.99, 110.98, 103.69, 122.32, 14.78, 42.74, 
4.04, 8.28, 84.96, 11.7, 49.97, 120.48, 52.6, 71.26, 16.3, 100.14, 
55.03, 6.51, 89.89, 51.71, 24.97, 55.66, 21.91, 81.48, 30.92, 
1.58, 7.52, 30.75, 3.45, 19.22, 5.42, 17.68, 45.54, 76.22, 125.34, 
83.62, 30.82, 90.32, 1.84, 19.98, 20.53, 32.59, 54.77, 2.3, 106.84, 
22.28, 45.18, 4.47, 39.66, 32.3, 16.23, 3.88, 2.23, 0.23, 18.73, 
0.79, 28.75, 79.54, 14.46, 15.15, 54.97, 48.59, 34.83, 58.42, 
35.29, 45.73, 57.53, 63.11, 65.05, 29.54, 77.21, 63.48, 83.35, 
34.3, 64.49, 29.54, 62.69, 21.62, 49.35, 99.02, 15.8, 41.89, 
12.98, 13.8, 43.6, 57.23, 31.38, 70.74, 39.46, 20.76, 67.22, 
127.15, 74.12, 1.97, 7.39, 25.17, 28.22, 14, 36.53, 20.83, 19.55, 
40.77, 27.76, 45.31, 34.46, 35.55, 26.94, 9.43, 10.51, 6.8, 8.18, 
8.02, 14.29, 6.11, 13.8, 4.9, 4.04, 14.82, 11.66, 73.07, 92.91, 
99.98, 10.64, 10.05, 95.8, 7.23, 12.81, 43.99, 13.9, 10.25, 16.36, 
18.2, 18.76, 12.32, 8.64, 11.79, 112.04, 70.97, 31.28, 28.85, 
21.49, 19.94, 22.14, 29.44, 67.62, 11.01, 45.24, 110.72, 20.24, 
14.06, 12.88, 31.51, 8.08, 13.08, 21.45, 24.28, 21.98, 32.89, 
23.26, 15.41, 15.41, 13.8, 40.12, 8.02, 15.77, 49.81, 18.17, 
24.21, 47.08, 6.6, 37.16, 13.01, 8.38, 14.36, 18.27, 17.28, 73.76, 
68.21, 22.83, 2.66, 69.06, 17.05, 8.61, 23.33, 13.34, 12.65, 
8.77, 128.92, 16.1, 4.99, 11.73, 22.97, 40.12, 20.37, 2.04, 45.73
), uiccc = structure(c(4L, 3L, 3L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 
2L, 1L, 4L, 2L, 1L, 2L, 3L, 1L, 2L, 3L, 2L, 1L, 2L, 3L, 2L, 4L, 
1L, 1L, 2L, 4L, 4L, 1L, 3L, 3L, 4L, 3L, 1L, 4L, 2L, 3L, 4L, 4L, 
4L, 3L, 2L, 4L, 1L, 4L, 2L, 4L, 4L, 2L, 4L, 4L, 1L, 4L, 2L, 3L, 
2L, 2L, 3L, 2L, 4L, 4L, 2L, 2L, 3L, 1L, 4L, 4L, 4L, 4L, 4L, 3L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 4L, 2L, 4L, 1L, 
2L, 1L, 1L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 3L, 3L, 4L, 1L, 
1L, 3L, 1L, 4L, 2L, 1L, 3L, 1L, 2L, 1L, 1L, 4L, 1L, 1L, 4L, 1L, 
1L, 3L, 2L, 2L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 4L, 4L, 
2L, 3L, 4L, 2L, 4L, 1L, 1L, 3L, 3L, 1L, 1L, 3L, 4L, 4L, 2L, 4L, 
4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 2L, 4L, 3L, 1L, 4L, 3L, 
4L, 4L, 3L, 1L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 1L, 4L, 4L, 2L, 
4L, 4L, 4L, 3L, 4L, 3L, 3L, 4L, 4L, 2L, 4L, 4L, 2L, 4L, 4L, 4L, 
4L, 1L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 
3L, 1L, 2L, 1L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 3L, 4L, 4L, 
1L, 3L, 3L, 4L, 3L), .Label = c("UICC Stage I", "UICC Stage II", 
"UICC Stage III", "UICC Stage IV"), class = "factor")), row.names = c(NA, 
-239L), class = "data.frame")

【问题讨论】:

    标签: r ggplot2 plot regression


    【解决方案1】:

    似乎缺少等于 8 的转移负担的值给rq.fit.fn 带来了一些麻烦。

    除了使用 geom_quantile 之外,您还可以计算绘图之外的中位数(分位数 = 0.5)并使用 geom_smooth 来获得回归线。

    请注意,它可能与lm 方法计算的线性回归和rq.fit.fn 计算的分位数回归存在一些差异:

    library(dplyr)
    Med <- p %>% group_by(n.sygdom) %>% summarise(Median = median(os.neck), Nb = n())
    
    # A tibble: 10 x 3
       n.sygdom Median    Nb
          <int>  <dbl> <int>
     1        0  36.5    141
     2        1  16.2     47
     3        2  16.2     22
     4        3   8.94     7
     5        4  12.6      9
     6        5  23.3      3
     7        6  17.9      4
     8        7   9.43     3
     9        9  11.2      2
    10       10   7.52     1
    

    这里是绘图部分

    library(ggplot2)
    ggplot(p, aes(x=n.sygdom,y=os.neck)) + 
      geom_point(aes(color=uiccc),shape=20, size=7,alpha=0.7) + 
      geom_point(data= Med, aes(y = Median), col = "black", size = 1.5, shape = 24)+
      geom_smooth(method = "lm", data = Med, aes(x = n.sygdom, y = Median), se = FALSE, linetype = 2, size =  1)+
      scale_fill_manual(values=cols) + 
      scale_colour_manual(values=cols) +
      scale_x_continuous(breaks = seq(0,18, by=1), name="Metastatic Burden") +
      scale_y_continuous(breaks = seq(0,200, by=25), limits=c(0,130), name="Time to death (months)") +
      theme(legend.position="none")
    

    【讨论】:

    • 非常感谢。你不仅帮我解决了很多问题,我也从你的回答中学到了很多东西。非常感谢!一个请求,似乎沿着p$n.sygdom 打印了几个小三角形。这些可以省略吗?
    • 不客气,我不确定lm fit 是否完全等同于quantile regression 算法。关于小三角形,我添加它们是为了向您展示每组的中位数在哪里。只需删除第二行 geom_point 就可以了。
    猜你喜欢
    • 2021-02-22
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2015-01-14
    • 2013-08-15
    • 2021-03-25
    • 2015-03-20
    • 2011-07-19
    相关资源
    最近更新 更多