重新创建ggplot的geom_smooth CI背景 - 在R基础上? [英] Recreate ggplot's geom_smooth CI background - in R basic?

查看:112
本文介绍了重新创建ggplot的geom_smooth CI背景 - 在R基础上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望重新创建此图:
alt text http://had.co.nz/stat405/resources/drills/plot-drills/ggplots/hrline7.png



(from 此处

使用R底图。



我不知道该怎么做。任何建议?

(我的动机是我想创建一个图线宽度(和/或颜色)将反映另一个维度。直到现在 - ggplot2是只有我在R中找到了我如何做到这一点的地方,我很乐意能够在R基础上做到这一点) 解决方案

好吧,我花了太多时间搞这个......注意最后一行是ggplot版本,所以你可以比较两者。

  #loess和误差曲线几乎就像ggplot2 
op< - par(las = 1,mar = c(3,3,1,1))
n < - 30
x< - sort(rnorm(n))#(预测器中变化的密度)
x< -x + abs(min(x))
x< -x / max (x)+ r norm(n)#(曲线)
m - - 黄土(y_x)
xx < - seq(min(x ),max(x),(max(x)-min(x))/ 1000)#增加值的密度以预测以提高曲线的质量
f< - 预测(m,xx,se = TRUE )
ci < - f $ se * qt(0.975,f $ df)
cih < - f $ fit + ci
(x,y,ylim = c(min(cil,y),max(cih,y)),cex.axis = 0.85,xlab ='',ylab ='',type ='n')
title(xlab ='x',ylab ='y',line = 2)
grid(col ='gray')
points (xx,f $ fit,col ='blue',lwd = 1.2)
xx < - c(xx,rev(xx),x,y,pch = 19,cex = 0.65) )
yy < - c(cil,rev(cih))
多边形(xx,yy,col = rgb(0.1,0.1,0.1,0.25),border = NA)
par (op)

#qplot(x,y,geom ='point')+ stat_smooth()


I wish to recreate this graph: alt text http://had.co.nz/stat405/resources/drills/plot-drills/ggplots/hrline7.png

(from here)

Using R base graphics.

I have no clue how to do that. Any advice ?

(My motivation is that I wish to create a plot where the line width (and/or color) will reflect another dimension. Until now - ggplot2 is the only place I found in R for how to do this. I would be happy to be able to do this also in base R)

解决方案

OK, I spent a little too much time messing with this... note the last line is the ggplot version so you can compare the two.

#loess and error curves almost just like ggplot2
op <- par(las=1, mar = c(3,3,1,1))
n <- 30
x <- sort(rnorm(n)) #(varying density in predictor)
x <- x + abs(min(x))
x <- x/max(x)*2*pi 
y <- sin(x)+rnorm(n) #(curvy)
m <- loess(y~x)
xx <- seq(min(x), max(x), (max(x)-min(x))/1000) #increase density of values to predict over to increase quality of curve
f <- predict(m, xx, se = TRUE)
ci <- f$se * qt(0.975, f$df)
cih <- f$fit + ci
cil <- f$fit - ci
plot(x,y, ylim = c(min(cil,y), max(cih,y)), cex.axis = 0.85, xlab = '', ylab = '', type = 'n')
title(xlab = 'x', ylab = 'y',line = 2)
grid(col = 'gray')
points(x,y, pch = 19, cex = 0.65)
lines(xx, f$fit, col = 'blue', lwd = 1.2)
xx <- c(xx, rev(xx))
yy <- c(cil, rev(cih))
polygon(xx, yy, col=rgb(0.1,0.1,0.1,0.25), border = NA)
par(op)

#qplot(x,y, geom = 'point') + stat_smooth()

这篇关于重新创建ggplot的geom_smooth CI背景 - 在R基础上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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