gpt4 book ai didi

r - ggplot2直方图binwidth

转载 作者:行者123 更新时间:2023-12-02 07:47:40 25 4
gpt4 key购买 nike

我想在一个图中创建多个直方图(使用facet_wrap)。这可能是一个示例代码:

df <- data.frame(p1 = rnorm(100,5,2), p2 = rnorm(100,80,20), group = rep(LETTERS[1:4],25))

library(ggplot2)
library(reshape)

plotData <- melt(df, id.vars = "group", measure.vars = c("p1","p2") )

m <- ggplot(plotData, aes(x = value, color = group, fill = group, group = group))
m <- m + geom_bar(position=position_dodge())
m <- m + facet_wrap( ~ variable,scales = "free_x")
print(m)

现在,我想修改它为每个参数(“p1,”p2”)创建的绘图,假设有 10 个 bin。

到目前为止,我找不到一种方法来执行此操作,因为 binwidth/breaks 计算应该依赖于数据子集。

这可能吗?

<小时/>

我想分享我的解决方案(取自上面链接的已回答问题),并通过将直方图与按直方图计数缩放的密度曲线重叠的可能性进行扩展:

df <- data.frame(p1 = rnorm(1000,5,2), p2 = rnorm(1000,80,20), group = rep(LETTERS[1:4],25))

library(ggplot2)
library(reshape)
library(plyr)

plotData <- melt(df, id.vars = "group", measure.vars = c("p1","p2") )

nBins <- 10

groupedData <- dlply(plotData, .(variable))
groupedBinWidth <- llply(groupedData, .fun = function(data, nBins) {
r <- range(data$value, na.rm = TRUE, finite = TRUE)
widthOfBins = (r[2] - r[1])/nBins
if (is.na(widthOfBins) || is.infinite(widthOfBins) || (widthOfBins <= 0)) widthOfBins <- NULL
widthOfBins
}, nBins = nBins)

densData <- dlply(plotData, .(variable, group), .fun = function(subData){
param <- subData$variable[1]
group <- subData$group[1]
d <- density(subData$value)
bw <- groupedBinWidth[[param]]
data.frame(x = d$x, y = d$y * nrow(subData) * bw , group = group, variable = param)
})

hls <- mapply(function(x, b) geom_bar(aes(x = value), position = position_dodge(), data = x, binwidth = b),
groupedData, groupedBinWidth)

dLay <- mapply(function(data) geom_density(data = data, aes(x = x, y = y), stat = "identity", fill = NA, size = 1),
densData)

m <- ggplot(plotData, aes(x = value, color = group, fill = group, group = group))
m <- m + hls
m <- m + dLay
m <- m + facet_wrap( ~ variable,scales = "free")
print(m)

enter image description here

最佳答案

试试这个 - 代码真的很难看,但如果我理解正确的话就可以了。您可能想使用 geom_密度 并删除填充以使其更具可读性。

nbin<- 5
m <- ggplot(plotData, aes(x = value, color = group, fill = group, group = group))
m <- m + geom_histogram(data = subset(plotData, variable == "p1"), binwidth=diff(range(subset(plotData, variable == "p1")$value))/nbin)
m <- m + geom_histogram(data = subset(plotData, variable == "p2"), binwidth=diff(range(subset(plotData, variable == "p2")$value))/nbin)
m <- m + facet_wrap( ~ variable,scales = "free_x")
print(m)

enter image description here

关于r - ggplot2直方图binwidth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277198/

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