18

我想绘制一个空心直方图,其中没有绘制垂直条,而只是一个轮廓。我找不到任何方法来做到这一点geom_histogramgeom_step+stat_bin组合似乎可以完成这项工作。但是,geom_step+stat_bin的 bin 会向右或向左移动半个 bin,具体取决于 step 的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")

在此处输入图像描述

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构建直方图的绘图对象以进行渲染。从此对象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))

在此处输入图像描述

编辑@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 回答
4

我今天早些时候回答了我自己的评论:这是使用 ggproto 为 v2 (ggplot2_2.0.0) 更新的@RosenMatev 答案的修改版本:

GeomStepHist <- ggproto("GeomStepHist", GeomPath,
                        required_aes = c("x"),

                        draw_panel = function(data, panel_scales, coord, direction) {
                          data <- as.data.frame(data)[order(data$x), ]

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

                          GeomPath$draw_panel(newdata, panel_scales, coord)
                        }
)


geom_step_hist <- function(mapping = NULL, data = NULL, stat = "bin",
                           direction = "hv", position = "stack", na.rm = FALSE, 
                           show.legend = NA, inherit.aes = TRUE, ...) {
  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomStepHist,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      direction = direction,
      na.rm = na.rm,
      ...
    )
  )
}
于 2016-01-18T16:16:15.313 回答
2

TLDR:使用geom_step(..., direction = "mid")

自从 Daniel Mastropietro 和 Dewey Dunnington 将“mid”作为ggplot2 v3.3.0的参数的附加选项实施后,这变得容易多了:directiongeom_step

library(ggplot2)

set.seed(1)
d <- data.frame(x = rnorm(1000))
ggplot(d, aes(x)) + 
  geom_histogram(breaks = seq(-4, 4, by=.5), color="red", fill = "transparent") +
  geom_step(stat="bin", breaks=seq(-4, 4, by=.5), color = "black", direction = "mid")

下面,作为参考,问题中的代码格式类似于上面的答案:

ggplot(d, aes(x)) + 
  geom_histogram(breaks = seq(-4, 4, by=.5), color = "red", fill = "transparent") +
  geom_step(stat="bin", breaks = seq(-4, 4, by=.5), color = "black", direction = "vh")

reprex 包(v0.3.0)于 2020 年 9 月 2 日创建

于 2020-09-02T16:51:16.810 回答
0

一种类似于@Rosen Matev 的简单方法(不适用于@julou 提到的ggplot2_2.0.0),我只想1)手动计算垃圾箱的值(使用如下所示的小函数)2)使用 geom_step() 希望这会有所帮助!

geom_step_hist<- function(d,binw){
  dd=NULL
  bin=min(d$y) # this enables having a first value that is = 0 (to have the left vertical bar of the plot when using geom_step)
  max=max(d$y)+binw*2 # this enables having a last value that is = 0 (to have the right vertical bar of the plot when using geom_step)
  xx=NULL
  yy=NULL
  while(bin<=max){
    n=length(temp$y[which(temp$y<bin & temp$y>=(bin-binw))])
    yy=c(yy,n)
    xx=c(xx,bin-binw)
    bin=bin+binw
    rm(n)
  }
  dd=data.frame(xx,yy)
  return(dd)
}
hist=ggplot(dd,aes(x=xx,y=yy))+
geom_step()
于 2016-10-25T13:51:44.313 回答