Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2'中指定密度的比例;s统计密度2D_R_Plot_Ggplot2_Kernel Density - Fatal编程技术网

R 在ggplot2'中指定密度的比例;s统计密度2D

R 在ggplot2'中指定密度的比例;s统计密度2D,r,plot,ggplot2,kernel-density,R,Plot,Ggplot2,Kernel Density,我希望创建多个密度图,以制作“动画热图” 因为动画的每一帧都应该是可比较的,所以我希望每个图形上的密度->颜色映射对于所有帧都是相同的,即使每个帧的数据范围都发生了变化 下面是我将用于每个单独图形的代码: 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=

我希望创建多个密度图,以制作“动画热图”

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

下面是我将用于每个单独图形的代码:

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)
假设我使用相同的代码,但“this_df”在每一帧上都会发生变化。所以在一张图中,密度可能在0到4e-4之间。另一方面,密度范围从0到4e-2

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

我能找到的最接近的是:

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

任何帮助都将不胜感激

编辑:

下面是一个可复制的示例:

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

因此,要使两个图显示具有相同级别的等高线,请使用
stat\u densit2d(…)
中的
breaks=…
参数。要使两个图具有相同的alpha到level的映射,请使用
scale\u alpha\u continuous(limits=…)

以下是要演示的完整代码:

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))
结果


我想为这个问题留下最新消息。截至2016年7月,
stat_density2d
不再中断。为了再现图形,需要将
breaks=1e-6*seq(0,10,by=2)
移动到
scale\u alpha\u continuous()


您正在将
alpha
映射到级别,而不是颜色。您可以通过添加
scale\u alpha\u continuous(limits=…)
来控制alpha比例,其中
limits
是一个向量,以
的单位指定限制。level..
,因此我认为(0,4e-2)。如果您提供数据集,可能会有人愿意为您提供更多帮助。感谢您的回复!我添加了带有虚假数据的可复制代码(无法共享真实数据)。请注意,如果在此代码末尾添加,例如
+scale\u alpha\u continuous(limits=c(0,2e-6))
,则会使alpha缩放连续,但不会固定轮廓。如何使两个图形的等高线缩放一致?您是说要为相同的..level.值绘制等高线。。在两个情节中?如果是这样,那么使用
stat\u density2d(…)
中的
breaks=…
。这就是我今天需要的解决方案。我运行了代码,发现当前ggplot2(ggplot2_2.1.0)的
stat_density2d
没有
中断。你能想出其他方法来达到同样的效果吗?
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))