Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Create a density plot in ggplot using frequency column in the data frame

$
0
0

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") 

enter image description here

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") 

enter image description here

For very large data set it is prohibitive. How should I do then?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>