为什么stat_density2d函数的bin参数未知? (ggmap) [英] Why is bins parameter unknown for the stat_density2d function? (ggmap)

查看:335
本文介绍了为什么stat_density2d函数的bin参数未知? (ggmap)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直坚持这几个小时。当我运行这个时:

  library(ggmap)
set.seed(1)
n = 100

df < - data.frame(x = rnorm(n,0,1),y = rnorm(n,0,1))

TestData< - ggplot (data = df)+
stat_density2d(aes(x = x,y = y,fill = as.factor(.. level ..)),bins = 4,geom =polygon,)+
geom_point(aes(x = x,y = y))+
scale_fill_manual(values = c(yellow,red,green,royalblue,black))

我得到这个错误信息:

  Error:Unknown parameters:bins 

有谁知道为什么?

解决方案

好的,加入这个作为第二个答案,因为我认为第一个答案中的描述和评论是有用的,我不觉得合并它们。基本上我认为必须有一个简单的方法来恢复退步功能。过了一段时间,并学习了一些关于 ggplot2 的基础知识,我通过重写了一些 ggplot2 函数来工作: p>

  library(ggmap)
library(ggplot2)

#------- ------------------------
#从stat-density-2d.R $开始复制

stat_density_2d< - 函数(mapping = NULL,data = NULL,geom =density_2d,
position =identity,contour = TRUE,
n = 100,h = NULL,na.rm = FALSE,bins = 0,
show.legend = NA,inherit.aes = TRUE,...){
图层(
data = data,
mapping = mapping,
stat = StatDensity2d,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list (
na.rm = na.rm,
contour =等值线,
n = n,
箱子=箱子,
...


}

s tat_density2d< - stat_density_2d

StatDensity2d< -
ggproto(StatDensity2d,Stat,
default_aes = aes(color =#3366FF,size = 0.5),

required_aes = c(x,y),

compute_group =函数(数据,比例,na.rm = FALSE,h = NULL,
如果(is.null(h)){
h <-c(MASS :: bandwidth.nrd(data $ x),MASS:等于= TRUE,n = 100,bins = 0) :data.yrd(data $ y))
}

dens< - MASS :: kde2d(
data $ x,data $ y,h = h,n = n,
lims = c(尺度$ x $ dimension(),尺度$ y $ dimension())

df < - data.frame(expand.grid(x = dens (等值)$ x,y = dens $ y),z = as.vector(dens $ z))
df $ group < - data $ group [1]


#StatContour $ compute_panel(df,scales,bins = bin,...)#坏点...
if( bins> 0){
StatContour $ compute_panel(df,scales,bins)
} else {
StatContour $ compute_panel(df,scales)
}
} else {
名称(df)< -c(x,y,density,group)
df $ level < - 1
df $ piece< - 1
df
}
}


#从stat-density-2d.R
# --------------------------

set.seed(1)
n = 100

df < - data.frame(x = rnorm(n,0,1),y = rnorm(n,0,1))

TestData< - ggplot(data = df)+
stat_density2d(aes(x = x,y = y,fill = as.factor(.. level ..)),bins = 5,geom =polygon)+
geom_point( aes(x = x,y = y))+
scale_fill_manual(values = c(yellow,red,green,royalblue,black))
print(TestData )

产生结果。请注意,改变 bins 参数现在具有所需的效果,不能通过改变 n 参数来复制。




I have been stuck with this for hours. When I run this :

library(ggmap)
set.seed(1)
n=100

df <- data.frame(x=rnorm(n, 0, 1), y=rnorm(n, 0, 1))

TestData <- ggplot (data = df) +
  stat_density2d(aes(x = x, y = y,fill = as.factor(..level..)),bins=4, geom = "polygon",) +
  geom_point(aes(x = x, y = y)) +
  scale_fill_manual(values = c("yellow","red","green","royalblue", "black"))

I get this error message :

Error: Unknown parameters: bins

Does anyone know why?

解决方案

Okay, adding this one as a second answer because I think the descriptions and comments in the first answer are useful and I don't feel like merging them. Basically I figured there must be an easy way to restore the regressed functionality. And after awhile, and learning some basics about ggplot2, I got this to work by overriding some ggplot2 functions:

library(ggmap)
library(ggplot2)

# -------------------------------
# start copy from stat-density-2d.R

stat_density_2d <- function(mapping = NULL, data = NULL, geom = "density_2d",
                            position = "identity", contour = TRUE,
                            n = 100, h = NULL, na.rm = FALSE,bins=0,
                            show.legend = NA, inherit.aes = TRUE, ...) {
  layer(
    data = data,
    mapping = mapping,
    stat = StatDensity2d,
    geom = geom,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      na.rm = na.rm,
      contour = contour,
      n = n,
      bins=bins,
      ...
    )
  )
}

stat_density2d <- stat_density_2d

StatDensity2d <- 
  ggproto("StatDensity2d", Stat,
          default_aes = aes(colour = "#3366FF", size = 0.5),

          required_aes = c("x", "y"),

          compute_group = function(data, scales, na.rm = FALSE, h = NULL,
                                   contour = TRUE, n = 100,bins=0) {
            if (is.null(h)) {
              h <- c(MASS::bandwidth.nrd(data$x), MASS::bandwidth.nrd(data$y))
            }

            dens <- MASS::kde2d(
              data$x, data$y, h = h, n = n,
              lims = c(scales$x$dimension(), scales$y$dimension())
            )
            df <- data.frame(expand.grid(x = dens$x, y = dens$y), z = as.vector(dens$z))
            df$group <- data$group[1]

            if (contour) {
              #  StatContour$compute_panel(df, scales,bins=bins,...) # bad dots...
              if (bins>0){
                StatContour$compute_panel(df, scales,bins)
              } else {
                StatContour$compute_panel(df, scales)
              }
            } else {
              names(df) <- c("x", "y", "density", "group")
              df$level <- 1
              df$piece <- 1
              df
            }
          }
  )

# end copy from stat-density-2d.R
# -------------------------------

set.seed(1)
n=100

df <- data.frame(x=rnorm(n, 0, 1), y=rnorm(n, 0, 1))

TestData <- ggplot (data = df) +
  stat_density2d(aes(x = x, y = y,fill = as.factor(..level..)),bins=5,geom = "polygon") +
  geom_point(aes(x = x, y = y)) +
  scale_fill_manual(values = c("yellow","red","green","royalblue", "black"))
print(TestData)

Which yields the result. Note that varying the bins parameter has the desired effect now, which cannot be replicated by varying the n parameter.

这篇关于为什么stat_density2d函数的bin参数未知? (ggmap)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆