使用直方图中断将函数应用于第二列 [英] Use histogram breaks to apply function over second column

查看:170
本文介绍了使用直方图中断将函数应用于第二列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多列数据,我们说x和时间。我想制作一个列x的直方图,并根据列时间中的值的聚合来绘制每个条的颜色,其中聚合按照直方图使用的分组进行分组。

I have multiple columns of data, let's say x and time. I want to make a histogram of column x, and color each bar based off an aggregation of the values in column time, where the aggregation is grouped by the breaks used for the histogram. So,

d = cbind(c(rep(1,3), rep(2,3)), c(10,20,10,20,10,20))
names(d) = c("x", "time")
hist(d[,"x"])

给我一​​个好的酒吧,让我想说,我想要这样的颜色:

Gives me a nice barplot, and let's say I want something like this for my colors:

palette(rainbow(25))
hist(d[,"x"], col=d[,"time"], n=10)

我想让col作为一个长度为10的向量,作为平均值)的时间列。

I would like to have the col be a vector of length 10 that is an aggregated function (such as mean) of the time column.

推荐答案

我将使用 plyr ggplot2

require(plyr)
require(ggplot2)

d <- data.frame(x=c(rep(1:4, each=4)), time=sample(10:100, 16, replace=T))
d <- ddply(d, .(x), transform, mean.time=mean(time))

ggplot(d, aes(x=x, group=x, fill=mean.time)) +
  geom_histogram()

这篇关于使用直方图中断将函数应用于第二列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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