Suppose I have a data frame with points coordinates (x,y) and frequency representing the number of occurrence of these points. How can I create a density plot, taking in account the frequency column?
library(ggplot2)
df <- data.frame(x = floor(runif(100, min=1, max=10)), y = floor(runif(100, min=1, max=10)), freq = floor(runif(100, min=100, max=1000)))
ggplot(df, aes(x, y)) + stat_density_2d(aes(fill = ..level..), geom = "polygon")
# the frequency column plays no rule. see:
df$freq <- NULL
ggplot(df, aes(x, y)) + stat_density_2d(aes(fill = ..level..), geom = "polygon")
I could replicate the data, but only for small data sets with small frequency values. In that case I could do the following:
df.expanded <- df[rep(row.names(df), df$freq), 1:2]
ggplot(df.expanded, aes(x, y)) + stat_density_2d(aes(fill = ..level..), geom = "polygon")
For very large data set it is prohibitive. How should I do then?