geom_step的空心直方图或分级

Vad*_*ich 17 r ggplot2 ggproto

我想绘制一个空心直方图,其中没有绘制垂直条,但只是一个轮廓.我找不到任何方法来做到这一点geom_histogram.在geom_step+ stat_bin组合看起来像它可能做的工作.但是,根据步骤的参数值,geom_step+ 的区间stat_bin向右或向左移动半个区间direction=.似乎正在做它的"步骤"WRT bin中心.有没有办法改变这种行为,所以它会在bin边缘做"步骤"?

这是一个例子:

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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ros*_*tev 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"
})
Run Code Online (Sandbox Code Playgroud)

这也适用于非均匀休息.为了说明用法:

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")
Run Code Online (Sandbox Code Playgroud)

情节


jor*_*ran 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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Hen*_*rik 11

还有一个.使用ggplot_build建直方图的剧情对象进行渲染.从此对象xy值中提取,以供使用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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑 @Vadim Khotilovich的以下评论(谢谢!)

xmin从剧情对象可以用来代替( - >不需要偏置调整)

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

p1 +
  geom_step(data = df, aes(x = xmin, y = y))   
Run Code Online (Sandbox Code Playgroud)


Bro*_*ieG 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")
Run Code Online (Sandbox Code Playgroud)

如果你有点乱,你可能会错过一些垃圾桶.只有(有点无意义)优势是它ggplot比@Joran的答案更多,尽管这是有争议的.

在此输入图像描述