18

内部に垂直バーがなく、アウトラインだけの中空のヒストグラムを描画したいと思います。でそれを行う方法が見つかりませんでしたgeom_histogramgeom_step+のstat_bin組み合わせは、仕事をすることができるように見えました。ただし、geom_step+のビンは、ステップのパラメーター値stat_binに応じて、右または左に半ビン分シフトされます。direction=WRTビンセンターの「ステップ」を実行しているようです。ビンの端で「ステップ」を実行するように、この動作を変更する方法はありますか?

以下に図を示します。

d <- data.frame(x=rnorm(1000))
qplot(x, data=d, geom="histogram",
      breaks=seq(-4,4,by=.5), color=I("red"), fill = I("transparent")) +
geom_step(stat="bin", breaks=seq(-4,4,by=.5), color="black", direction="vh")

ここに画像の説明を入力

4

7 に答える 7

12

次のように新しい Geom を作成することを提案します。

library(ggplot2)
library(proto)

geom_stephist <- function(mapping = NULL, data = NULL, stat="bin", position="identity", ...) {
  GeomStepHist$new(mapping=mapping, data=data, stat=stat, position=position, ...)
}

GeomStepHist <- proto(ggplot2:::Geom, {
  objname <- "stephist"

  default_stat <- function(.) StatBin
  default_aes <- function(.) aes(colour="black", size=0.5, linetype=1, alpha = NA)

  reparameterise <- function(., df, params) {
    transform(df,
              ymin = pmin(y, 0), ymax = pmax(y, 0),
              xmin = x - width / 2, xmax = x + width / 2, width = NULL
    )
  }

  draw <- function(., data, scales, coordinates, ...) {
    data <- as.data.frame(data)[order(data$x), ]

    n <- nrow(data)
    i <- rep(1:n, each=2)
    newdata <- rbind(
      transform(data[1, ], x=xmin, y=0),
      transform(data[i, ], x=c(rbind(data$xmin, data$xmax))),
      transform(data[n, ], x=xmax, y=0)
    )
    rownames(newdata) <- NULL

    GeomPath$draw(newdata, scales, coordinates, ...)
  }
  guide_geom <- function(.) "path"
})

これは、不均一な休憩にも機能します。使用法を説明するには:

d <- data.frame(x=runif(1000, -5, 5))
ggplot(d, aes(x)) +
  geom_histogram(breaks=seq(-4,4,by=.5), color="red", fill=NA) +
  geom_stephist(breaks=seq(-4,4,by=.5), color="black")

プロット

于 2014-05-15T21:29:28.290 に答える
11

これは理想的ではありませんが、私が思いつくことができる最高のものです:

h <- hist(d$x,breaks=seq(-4,4,by=.5))
d1 <- data.frame(x = h$breaks,y = c(h$counts,NA))

ggplot() + 
    geom_histogram(data = d,aes(x = x),breaks = seq(-4,4,by=.5),
                                 color = "red",fill = "transparent") + 
    geom_step(data = d1,aes(x = x,y = y),stat = "identity")

ここに画像の説明を入力

于 2014-05-15T18:37:50.597 に答える
11

さらに別のもの。ggplot_buildレンダリング用のヒストグラムのプロット オブジェクトを構築するために使用します。このオブジェクトxからy値が抽出され、 に使用されgeom_stepます。by値をオフセットするために使用しxます。

by <- 0.5
p1 <- ggplot(data = d, aes(x = x)) +
  geom_histogram(breaks = seq(from = -4, to = 4, by = by),
                 color = "red", fill = "transparent")

df <- ggplot_build(p1)$data[[1]][ , c("x", "y")]

p1 +
  geom_step(data = df, aes(x = x - by/2, y = y))

ここに画像の説明を入力

@Vadim Khotilovich からの次のコメントを編集します (ありがとう!)

代わりに、プロット オブジェクトのxminを使用できます (-> オフセット調整は不要です)。

df <- ggplot_build(p1)$data[[1]][ , c("xmin", "y")]

p1 +
  geom_step(data = df, aes(x = xmin, y = y))   
于 2014-05-15T19:00:50.730 に答える
7

理想的とは言えない別の方法:

qplot(x, data=d, geom="histogram", breaks=seq(-4,4,by=.5), color=I("red"), fill = I("transparent")) +
  stat_summary(aes(x=round(x * 2 - .5) / 2, y=1), fun.y=length, geom="step")

少しいじれば、おそらく元に戻すことができるいくつかのビンがありません。(やや無意味な)唯一の利点はggplot、@ Joranの回答よりも多いことですが、それでも議論の余地があります。

ここに画像の説明を入力

于 2014-05-15T18:46:21.273 に答える