Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2d color plot in R

Tags:

r

ggplot2

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?

like image 577
aplavin Avatar asked Dec 17 '25 14:12

aplavin


2 Answers

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

like image 189
Roland Avatar answered Dec 20 '25 07:12

Roland


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

like image 22
plannapus Avatar answered Dec 20 '25 06:12

plannapus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!