简体   繁体   中英

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 (eg 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?

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

The way I'm doing is using a small alpha (ie 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)

在此输入图像描述

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)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM