ggplot2上的多面直方图中的各个binwidth [英] Individual binwidths in faceted histogram on ggplot2

查看:496
本文介绍了ggplot2上的多面直方图中的各个binwidth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用facet_grid做一系列直方图,并且我希望网格中的每个直方图都具有相同数量的类,例如下面的示例. 6节课.以下示例中的问题是binwidth = diff(range(x$data))/6)根据a,b和c的整体范围定义类,即为所有三个方面定义一个binwidth.

I do a series of histograms with facet_grid and I want every histogram in the grid to have the same number of classes, in the example below e.g. 6 classes. The problem in this example below is that binwidth = diff(range(x$data))/6) defines the classes according to the overall range of a, b and c, i.e. defines one binwidth for all three facets.

如何分别定义面a,b和c的binwidth?

How do I define binwidth individually for the facets a, b and c?

require("ggplot2")

a <- c(1.21,1.57,1.21,0.29,0.36,0.29,0.93,0.26,0.28,0.48,
       0.12,0.38,0.83,0.82,0.41,0.69,0.25,0.98,0.52,0.11)
b <- c(0.42,0.65,0.17,0.38,0.44,0.01,0.01,0.03,0.15,0.01)
c <- c(1.09,3.55,1.07,4.55,0.55,0.11,0.72,0.66,1.22,3.04,
       2.01,0.64,0.47,1.33,3.44)

x <- data.frame(data = c(a,b,c), variable = c(rep("a",20),rep("b",10),rep("c",15)),area="random")

qplot(data, data = x, geom = "histogram", binwidth = diff(range(x$data))/6) +
  facet_grid(area~variable, scales = "free")

推荐答案

这不是最佳选择,但是您可以在不同的图层中进行直方图绘制:

This is not optimal but you can do the histogram in different layers:

ggplot(x, aes(x=data)) +
   geom_histogram(data=subset(x, variable=="a"), binwidth=.1) +
   geom_histogram(data=subset(x, variable=="b"), binwidth=.2) +
   geom_histogram(data=subset(x, variable=="c"), binwidth=.5) +
   facet_grid(area~variable, scales="free")

这篇关于ggplot2上的多面直方图中的各个binwidth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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