简体   繁体   English

在R中按分位数绘制

[英]Plotting by quantile in R

I want to plot foo ~ bar . 我想绘制foo ~ bar However, I don't want to look at the exact data, I'd rather break bar into say quantiles, and plot mean(foo) for every quantile (so my final plot will have 5 data points). 但是,我不想查看确切的数据,我宁愿将分bar分成多个分位数,并为每个分位数绘制mean(foo) (这样我的最终图将有5个数据点)。 Is this possible? 这可能吗?

 foo <- rnorm(100)
 bar <- rnorm(100)

  mn.foo.byQ10bar <- tapply(foo, cut(bar, quantile(bar, (0:5)/5, na.rm=TRUE)), mean)

> mn.foo.byQ5bar
 (-3.31,-0.972] (-0.972,-0.343]  (-0.343,0.317]   (0.317,0.792]    (0.792,2.71] 
     0.13977839      0.03281258     -0.18243804     -0.14242885     -0.01696712 

 plot(mn.foo.byQ5bar)

This is a fairly standard task and Harrell's Hmisc package's cut2 function has a nice gr= argument that lets you do this by just specifying an integer for the number of groups. 这是一个相当标准的任务,Harrell的Hmisc程序包的cut2函数具有一个不错的gr =参数,您可以通过仅为组数指定一个整数来执行此操作。 I also like it because the intervals from the cut operation are left-closed instead of R default for right-closed. 我也喜欢它,因为剪切操作的间隔是左关闭的,而不是右关闭的R default。

You can combine a lot of these lines into more concise code, but here it is broken down 您可以将许多这些行组合成更简洁的代码,但此处已将其细分

# Sample Data: 
x <- 1:100;   y <- rnorm(x)

# Number Of Groups
N <- 5

# quantiles
Q.y <- quantile(y, probs=seq(0, 1, length=(N+1)))
Q.x <- quantile(x, probs=seq(0, 1, length=N))

# means of y by quantile
means.y <- c(by(y, cut(y, Q.y), mean))

# plot them 
qplot(Q.x, means.y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM