2D Histogram in R: Converting from Count to Frequency within a Column

Posted by Jac on Stack Overflow See other posts from Stack Overflow or by Jac
Published on 2014-05-27T02:13:16Z Indexed on 2014/05/27 3:28 UTC
Read the original article Hit count: 184

Filed under:
|
|
|

Would appreciate help with generating a 2D histogram of frequencies, where frequencies are calculated within a column. My main issue: converting from counts to column based frequency.

Here's my starting code:

# expected packages
library(ggplot2)
library(plyr)

# generate example data corresponding to expected data input
x_data = sample(101:200,10000, replace = TRUE)
y_data = sample(1:100,10000, replace = TRUE)
my_set = data.frame(x_data,y_data)

# define x and y interval cut points
x_seq = seq(100,200,10)
y_seq = seq(0,100,10)

# label samples as belonging within x and y intervals
my_set$x_interval = cut(my_set$x_data,x_seq)
my_set$y_interval = cut(my_set$y_data,y_seq)

# determine count for each x,y block
xy_df = ddply(my_set, c("x_interval","y_interval"),"nrow") # still need to convert for use with dplyr

# convert from count to frequency based on formula: freq = count/sum(count in given x interval)
################ TRYING TO FIGURE OUT #################

# plot results
fig_count <- ggplot(xy_df, aes(x = x_interval, y = y_interval)) + geom_tile(aes(fill = nrow)) # count
fig_freq <- ggplot(xy_df, aes(x = x_interval, y = y_interval)) + geom_tile(aes(fill = freq)) # frequency

I would appreciate any help in how to calculate the frequency within a column.

Thanks! jac

EDIT: I think the solution will require the following steps 1) Calculate and store overall counts for each x-interval factor 2) Divide the individual bin count by its corresponding x-interval factor count to obtain frequency.

Not sure how to carry this out though. .

© Stack Overflow or respective owner

Related posts about r

    Related posts about frequency