在ggplot2的stat_density2d中指定密度的比例

时间:2021-01-28 14:58:10

I'm looking to create multiple density graphs, to make an "animated heat map."

我正在寻找创建多个密度图,以制作“动画热图”。

Since each frame of the animation should be comparable, I'd like the density -> color mapping on each graph to be the same for all of them, even if the range of the data changes for each one.

由于动画的每个帧都应该是可比较的,我希望每个图上的密度 - >颜色映射对于所有这些都是相同的,即使每个帧的数据范围发生变化。

Here's the code I'd use for each individual graph:

这是我为每个单独的图表使用的代码:

ggplot(data= this_df, aes(x=X, y=Y) ) + 
    geom_point(aes(color= as.factor(condition)), alpha= .25) +
    coord_cartesian(ylim= c(0, 768), xlim= c(0,1024)) + scale_y_reverse() +
    stat_density2d(mapping= aes(alpha = ..level..), geom="polygon", bins=3, size=1)

Imagine I use this same code, but 'this_df' changes on each frame. So in one graph, maybe density ranges from 0 to 4e-4. On another, density ranges from 0 to 4e-2.

想象一下,我使用相同的代码,但'this_df'在每一帧上都会发生变化。因此,在一个图中,密度范围可以从0到4e-4。另一方面,密度范围为0到4e-2。

By default, ggplot will calculate a distinct density -> color mapping for each of these. But this would mean the two graphs-- the two frames of the animation--aren't really comparable. If this were a histogram or density plot, I'd simply make a call to coord_cartesian and change the x and y lim. But for the density plot, I have no idea how to change the scale.

默认情况下,ggplot将计算每个密度的不同密度 - >颜色映射。但这意味着两个图形 - 动画的两个框架 - 并不具有可比性。如果这是直方图或密度图,我只需调用coord_cartesian并更改x和y lim。但对于密度图,我不知道如何改变比例。

The closest I could find is this:

我能找到的最接近的是:

Overlay two ggplot2 stat_density2d plots with alpha channels

使用Alpha通道叠加两个ggplot2 stat_density2d图

But I don't have the option of putting the two density plots on the same graph, since I want them to be distinct frames.

但我没有选择将两个密度图放在同一个图上,因为我希望它们是不同的帧。

Any help would be hugely appreciated!

任何帮助将非常感谢!

EDIT:

Here's a reproducible example:

这是一个可重复的例子:

set.seed(4)
g = list(NA,NA)
for (i in 1:2) {

  sdev = runif(1)
  X = rnorm(1000, mean = 512, sd= 300*sdev)
  Y = rnorm(1000, mean = 384, sd= 200*sdev)

  this_df = as.data.frame( cbind(X = X,Y = Y, condition = 1:2) )

  g[[i]] = ggplot(data= this_df, aes(x=X, y=Y) ) + 
    geom_point(aes(color= as.factor(condition)), alpha= .25) +
    coord_cartesian(ylim= c(0, 768), xlim= c(0,1024)) + scale_y_reverse() +
    stat_density2d(mapping= aes(alpha = ..level.., color= as.factor(condition)), geom="contour", bins=4, size= 2) 

}
print(g) # level has a different scale for each

2 个解决方案

#1


7  

So to have both plots show contours with the same levels, use the breaks=... argument in stat_densit2d(...). To have both plots with the same mapping of alpha to level, use scale_alpha_continuous(limits=...).

因此,要让两个图都显示具有相同级别的轮廓,请使用stat_densit2d(...)中的breaks = ...参数。要使两个绘图具有相同的alpha到level的映射,请使用scale_alpha_continuous(limits = ...)。

Here is the full code to demonstrate:

以下是演示的完整代码:

library(ggplot2)
set.seed(4)
g = list(NA,NA)
for (i in 1:2) {
  sdev = runif(1)
  X = rnorm(1000, mean = 512, sd= 300*sdev)
  Y = rnorm(1000, mean = 384, sd= 200*sdev)
  this_df = as.data.frame( cbind(X = X,Y = Y, condition = 1:2) )

  g[[i]] = ggplot(data= this_df, aes(x=X, y=Y) ) + 
    geom_point(aes(color= as.factor(condition)), alpha= .25) +
    coord_cartesian(ylim= c(0, 768), xlim= c(0,1024)) + scale_y_reverse() +
    stat_density2d(mapping= aes(alpha = ..level.., color= as.factor(condition)), 
                   breaks=1e-6*seq(0,10,by=2),geom="contour", bins=4, size= 2)+
    scale_alpha_continuous(limits=c(0,1e-5))+
    scale_color_discrete("Condition")
}
library(gridExtra)
do.call(grid.arrange,c(g,ncol=2))

And the result...

结果......

在ggplot2的stat_density2d中指定密度的比例

#2


8  

I would like to leave an update for this question. As of July 2016, stat_density2d is not taking breaks any more. In order to reproduce the graphic, you need to move breaks=1e-6*seq(0,10,by=2) to scale_alpha_continuous().

我想留下这个问题的更新。截至2016年7月,stat_density2d不再休息。为了重现图形,您需要将breaks = 1e-6 * seq(0,10,by = 2)移动到scale_alpha_continuous()。

set.seed(4)
g = list(NA,NA)
for (i in 1:2) {
    sdev = runif(1)
    X = rnorm(1000, mean = 512, sd= 300*sdev)
    Y = rnorm(1000, mean = 384, sd= 200*sdev)
    this_df = as.data.frame( cbind(X = X,Y = Y, condition = 1:2) )

g[[i]] = ggplot(data= this_df, aes(x=X, y=Y) ) +
         geom_point(aes(color= as.factor(condition)), alpha= .25) +
         coord_cartesian(ylim= c(0, 768), xlim= c(0,1024)) +
         scale_y_reverse() +
         stat_density2d(mapping= aes(alpha = ..level.., color= as.factor(condition)),
         geom="contour", bins=4, size= 2) +
         scale_alpha_continuous(limits=c(0,1e-5), breaks=1e-6*seq(0,10,by=2))+
         scale_color_discrete("Condition")
    }

do.call(grid.arrange,c(g,ncol=2))

#1


7  

So to have both plots show contours with the same levels, use the breaks=... argument in stat_densit2d(...). To have both plots with the same mapping of alpha to level, use scale_alpha_continuous(limits=...).

因此,要让两个图都显示具有相同级别的轮廓,请使用stat_densit2d(...)中的breaks = ...参数。要使两个绘图具有相同的alpha到level的映射,请使用scale_alpha_continuous(limits = ...)。

Here is the full code to demonstrate:

以下是演示的完整代码:

library(ggplot2)
set.seed(4)
g = list(NA,NA)
for (i in 1:2) {
  sdev = runif(1)
  X = rnorm(1000, mean = 512, sd= 300*sdev)
  Y = rnorm(1000, mean = 384, sd= 200*sdev)
  this_df = as.data.frame( cbind(X = X,Y = Y, condition = 1:2) )

  g[[i]] = ggplot(data= this_df, aes(x=X, y=Y) ) + 
    geom_point(aes(color= as.factor(condition)), alpha= .25) +
    coord_cartesian(ylim= c(0, 768), xlim= c(0,1024)) + scale_y_reverse() +
    stat_density2d(mapping= aes(alpha = ..level.., color= as.factor(condition)), 
                   breaks=1e-6*seq(0,10,by=2),geom="contour", bins=4, size= 2)+
    scale_alpha_continuous(limits=c(0,1e-5))+
    scale_color_discrete("Condition")
}
library(gridExtra)
do.call(grid.arrange,c(g,ncol=2))

And the result...

结果......

在ggplot2的stat_density2d中指定密度的比例

#2


8  

I would like to leave an update for this question. As of July 2016, stat_density2d is not taking breaks any more. In order to reproduce the graphic, you need to move breaks=1e-6*seq(0,10,by=2) to scale_alpha_continuous().

我想留下这个问题的更新。截至2016年7月,stat_density2d不再休息。为了重现图形,您需要将breaks = 1e-6 * seq(0,10,by = 2)移动到scale_alpha_continuous()。

set.seed(4)
g = list(NA,NA)
for (i in 1:2) {
    sdev = runif(1)
    X = rnorm(1000, mean = 512, sd= 300*sdev)
    Y = rnorm(1000, mean = 384, sd= 200*sdev)
    this_df = as.data.frame( cbind(X = X,Y = Y, condition = 1:2) )

g[[i]] = ggplot(data= this_df, aes(x=X, y=Y) ) +
         geom_point(aes(color= as.factor(condition)), alpha= .25) +
         coord_cartesian(ylim= c(0, 768), xlim= c(0,1024)) +
         scale_y_reverse() +
         stat_density2d(mapping= aes(alpha = ..level.., color= as.factor(condition)),
         geom="contour", bins=4, size= 2) +
         scale_alpha_continuous(limits=c(0,1e-5), breaks=1e-6*seq(0,10,by=2))+
         scale_color_discrete("Condition")
    }

do.call(grid.arrange,c(g,ncol=2))