1

私は何時間もこれにこだわっています。これを実行すると:

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

誰かが理由を知っていますか?

4

2 に答える 2

3

さて、最初の回答の説明とコメントは役に立ち、それらをマージしたくないので、これを2番目の回答として追加します。基本的に、私は後退した機能を復元する簡単な方法があるに違いないと考えました. そしてしばらくして、 に関するいくつかの基本を学び、いくつかの関数ggplot2をオーバーライドしてこれを機能させました。ggplot2

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)

結果が得られます。パラメーターを変更すると、必要な効果が得られることに注意してください。binsこれは、パラメーターを変更しても再現できませんn

ここに画像の説明を入力

于 2015-12-29T14:56:52.430 に答える
0

アップデート:

Roland との長い議論 (コメントを参照) の後、彼はおそらく回帰バグであると判断し、バグレポートを提出しました。


「なぜbinsパラメータが不明なのですか?」という質問で、かなり時間をかけて調べたのでお答えします。

あなたの例は、パラメーターが使用されている2013年10月のこのリンクから明らかに来ています。ggplot の stat_density2d を正しく解釈する方法

ただし、これは文書化されたパラメーターではなく、そこで使用されているかどうかは明らかではありません。おそらく、 によって使用されている他のライブラリ (MASS など) に渡されるパラメータですstat_density2d

scale_fill_manual呼び出しを取り除き、これを使用することで、コードを機能させることができます。

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..)), geom = "polygon",) +
  geom_point(aes(x = x, y = y)) 
#  scale_fill_manual(values = c("yellow","red","green","royalblue", "black"))
print(TestData)

これにより、次のようになります。 ここに画像の説明を入力

これは元の 2013 年 10 月のリンクに投稿されたプロットとはかなり異なっているように見えるため、stat_density2dそれ以降大幅に書き直されたか、またはおそらくMASS:kde2d(または別の MASS ルーチン)、binパラメーターが受け入れられなくなったと言えます。また、そのパラメーターが何かをしたことも明らかではありません (リンクを読んでください)。

ただし、パラメーターnを変更することはできます。hまた、観点からは文書化されていませんstat_density2d(私が知る限り)。

于 2015-12-22T13:07:23.697 に答える