gpt4 book ai didi

r - ggplot2 上的分面直方图中的单个 binwidths

转载 作者:行者123 更新时间:2023-12-01 00:59:37 25 4
gpt4 key购买 nike

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

我如何为方面 a、b 和 c 单独定义 binwidth?

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

最佳答案

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

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

关于r - ggplot2 上的分面直方图中的单个 binwidths,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24570646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com