简体   繁体   English

R中的2d颜色图

[英]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. 我需要一个二维图:x轴代表天,y轴代表一天的时间(例如小时),这一天这个小时的事件数用颜色表示(或者换句话说? )相应的细胞。

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). 但由于大量事件(每月超过100万),可能只能看到特定时间内发生事件的事实,而不是有多少事件(几乎所有细胞都充满了黑色)。 So, the question is - how to create such a plot in R? 所以,问题是 - 如何在R中创建这样的情节?

You could make a hexbin plot: 你可以制作一个hexbin图:

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: 我正在做的方法是为每个事件使用一个小的alpha(即透明度),以便叠加事件具有更高(累积)的alpha,从而给出叠加事件数量的概念:

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)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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