用于geom_step的空心直方图或分级 [英] Hollow histogram or binning for geom_step

查看:208
本文介绍了用于geom_step的空心直方图或分级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画一个空心直方图,里面没有竖条,而只是一个轮廓。我找不到用 geom_histogram 的方法。 geom_step + stat_bin 组合似乎可以完成这项工作。但是, geom_step + stat_bin 的元素会向右或向左移动半个元素,具体取决于在步骤的 direction = 参数值上。它似乎正在执行步骤WRT垃圾箱中心。有什么方法可以改变这种行为,所以它会做bin边的steps?



下面是一个例子:

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

解决方案

  library(ggplot2)
library( proto)

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

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

default_stat< - 功能(。)StatBin
default_aes< - function(。)aes(color =black,size = 0.5,linetype = 1,alpha = NA)

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

}

绘制< - 函数(。,数据,比例,坐标,...){
data < - as 。数据帧(数据)[order(data $ x),]

n < - nrow(data)
i < - rep(1:n,each = 2)$ b (数据[1,],x = xmin,y = 0),
变换(数据[i,],x = c(rbind(数据$ xmin ,数据$ xmax))),
变换(数据[n,],x = xmax,y = 0)

rownames(newdata)< - NULL

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

这也适用于非统一休息。为了说明用法:

pre $ 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 (break = seq(-4,4 by = .5),color =black)


I would like to draw a hollow histogram that has no vertical bars drawn inside of it, but just an outline. I couldn't find any way to do it with geom_histogram. The geom_step+stat_bin combination seemed like it could do the job. However, the bins of geom_step+stat_bin are shifted by a half bin either to the right or to the left, depending on the step's direction= parameter value. It seems like it is doing its "steps" WRT bin centers. Is there any way to change this behavior so it would do the "steps" at bin edges?

Here's an illustration:

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")

解决方案

I propose making a new Geom like so:

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"
})

This also works for non-uniform breaks. To illustrate the usage:

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")

这篇关于用于geom_step的空心直方图或分级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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