aplavin
aplavin

Reputation: 2229

2d color plot in R

I have a data frame with many events, each of them having a timestamp.

I need a 2-dimensional plot of this: x axis represents days, y axis represents the time of a day (e.g. hours), and the number of events in this hour of this day is represented by the color (or maybe another way?) of the corresponding cell.

First I've tried to use

     ggplot(events) + 
      geom_jitter(aes(x = round(TimeStamp / (3600*24)), 
                      y = TimeStamp %% (3600*24))), 

but due to a large number of events (more than 1 million per month) it's possible to see only the fact that there were events during a specific hour, not how many there were (almost all cells are just filled with black). So, the question is - how to create such a plot in R?

Upvotes: 3

Views: 2767

Answers (2)

plannapus
plannapus

Reputation: 18749

The way I'm doing is using a small alpha (i. e. transparency) for each event so that superimposing events have an higher (cumulated) alpha, giving thus an idea of the number of superimposed events:

library(ggplot2)
events <- data.frame(x=round(rbinom(1000,1000, 0.1)),y=round(rnorm(1000,10,3)))
ggplot(events)
+ geom_point(aes(x=x, y=y), colour="black", alpha=0.2)

enter image description here

Another solution would be to represent it as an heatmap:

 hm <- table(events)
 xhm <- as.numeric(rownames(hm))
 yhm <- as.numeric(colnames(hm))
 image(xhm,yhm,hm)

enter image description here

Upvotes: 2

Roland
Roland

Reputation: 132706

You could make a hexbin plot:

set.seed(42)
events <- data.frame(x=round(rbinom(1000,1000, 0.1)),y=round(rnorm(1000,10,3)))
library(ggplot2)
library(hexbin)
p1 <- ggplot(events,aes(x,y)) + geom_hex()
print(p1)

hexbin plot

Upvotes: 3

Related Questions