【问题标题】:Show loess smoothed trend-line in line plot, #ggplot2.在线图中显示黄土平滑趋势线,#ggplot2。
【发布时间】:2017-01-08 18:58:32
【问题描述】:

如何在图中显示黄土平滑趋势线?请帮助处理警告消息:“已删除 19 行包含非有限值 (stat_smooth)”。

我的数据:

yrcnt<-read.table(header = TRUE, text = "year outcome pop rate pred.SC
1  1995    2306  87592001 2.632660 0.9626214
2  1996    2221  87628543 2.534562 0.9599941
3  1997    2202  81872629 2.689544 0.9573667
4  1998    2316  88200076 2.625848 0.9547394
5  1999    2456  96200312 2.553006 0.9521121
6  2000    2526  99565063 2.537035 0.9494848
7  2001    2511  95951330 2.616952 0.9468575
8  2002    2537  96976191 2.616106 0.9442302
9  2003    2618 101673130 2.574918 0.9416028
10 2004    2644 104554479 2.528825 0.9389755
11 2005    2594 100522055 2.580528 0.9363482
12 2006    2620 105787278 2.476668 0.9337209
13 2007    2722 108946407 2.498476 0.9310936
14 2008    2788 112200567 2.484836 0.9284663
15 2009    2706 104491560 2.589683 0.9258389
16 2010    2773 108651896 2.552187 0.9232116
17 2011    2764 109632577 2.521148 0.9205843
18 2012    2694 107594922 2.503836 0.9179570
19 2013    2673 107553219 2.485281 0.9153297")

http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html 

我的代码:

    p1 <- ggplot(yrcnt, aes(y = log(rate), x = year))
    yrcnt$pred.SC <- predict(lm(year ~ log(rate), data = yrcnt))
    p1 + geom_line(aes(color = rate)) +geom_line(aes(y = pred.SC))
    p1 + geom_line(aes(color = rate)) + geom_smooth()
    p4 <- p1 + geom_line(aes(color = rate)) + geom_smooth(color="red")
    p4 + scale_x_continuous(name = "Years",limits = c(1995, 2013),breaks = 1994:2014) +
      scale_y_continuous(name = "Pancreatic Cancer Hospitalization Rate, 1995-2013",limits = c(2.4, 2.7),breaks = seq(2.4, 2.7, by = 0.1)) +
      ggtitle("Long Term Trend in Pancreatic Cancer Hospitalizations")
>`geom_smooth()` using method = 'loess'
p4=Base plot with trendline

未能合并到基础 plot=p4 的比例图

【问题讨论】:

  • 确实意识到我们无法引用您硬盘上的 html 文件,不是吗?
  • 您选择的 y 范围内没有任何数据是正确的。这也是一个警告,只是告诉您某些数据点无法绘制 - 不是错误
  • 说真的,孩子们最近有什么问题,op 提供了数据、代码、可重现的示例、图像、描述了所需的输出...... OP 还能做些什么来取悦你?
  • G5W,刚刚编辑。谢谢。
  • rawr - 我刚刚添加了我在此处复制代码时监督的 y 轴提要

标签: r ggplot2


【解决方案1】:

在对scale_y_continuous()的函数调用中,删除参数

                       limits = c(2.4, 2.7), 
                       breaks = seq(2.4, 2.7, 
                                    by = 0.1)) 

因为 y 轴的真实范围介于 0.9 和 1 之间,而您将它们设置为介于 2.4 和 2.7 之间的范围。我不知道您是否需要这里的速率或日志(速率)。

另一种选择是

library('ggplot2')
p1 <- ggplot(yrcnt, aes(y = rate, x = year))

######### lm() args flipped, then 
######### wrapped in exp() function.
yrcnt$pred.SC <- exp(predict(lm( log(rate) ~ year, data = yrcnt))) 

p1 + geom_line(aes(color = rate)) +geom_line(aes(y = pred.SC))

p4 <- p1 + geom_line(aes(color = rate)) + geom_smooth(color="red", method="loess")
p4 + scale_x_continuous(name = "Years",limits = c(1995, 2013),breaks = 1994:2014) +
        scale_y_continuous(name = "Pancreatic Cancer Hospitalization Rate, 1995-2013",limits = c(2.4, 2.7),breaks = seq(2.4, 2.7, by = 0.1)) +
        theme(legend.position ="none") +
        ggtitle("Long Term Trend in Pancreatic Cancer Hospitalizations")

【讨论】:

  • 我可以看到我搞砸了。谢谢。
猜你喜欢
  • 2021-05-08
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多