是否可以根据计算值在ggplot中绘制直方图(每个bin的计数和每个bin的范围)?

问题描述 投票:0回答:1

我想知道是否可以根据其他地方计算的值在 ggplot 中绘制直方图。

我有另一个软件(EcoSim 7.1)的分析输出中每个箱的计数以及每个箱的最小和最大范围。我想要的只是使用 EcoSim 数据输出绘制直方图。

以下是Ecosim生成的模拟频率的实际示例:

直方图低点 直方图高位 # 模拟
1.42416 1.43086 443
1.43086 1.43756 1672
1.43756 1.44426 2679
1.44426 1.45096 2263
1.45096 1.45766 1546
1.45766 1.46436 857
1.46436 1.47106 320
1.47106 1.47776 112
1.47776 1.48446 39
1.48446 1.49116 19
1.49116 1.49785 33
1.49785 1.50455 16

这是我尝试过但没有成功的代码:

histmin <- c(1.424,1.431,1.438,1.444,1.451,1.458,1.464,1.471,1.478,1.484,1.491,1.498)
histmax <- c(1.431,1.438,1.444,1.451,1.458,1.464,1.471,1.478,1.484,1.491,1.498,1.505)
freq <- c(443,1672,2679,2263,1546,857,320,112,39,19,33,16)
ecosimhist <- data.frame(histmin, histmax, freq)
ggplot(ecosimhist, aes(x=freq)) + geom_histogram(bins=12)
r ggplot2 histogram
1个回答
0
投票

您可以计算每个 bin 的中心点并使用

geom_col
,如下所示:

library(tidyverse)

histmin <- c(1.424,1.431,1.438,1.444,1.451,1.458,1.464,1.471,1.478,1.484,1.491,1.498)
histmax <- c(1.431,1.438,1.444,1.451,1.458,1.464,1.471,1.478,1.484,1.491,1.498,1.505)
freq <- c(443,1672,2679,2263,1546,857,320,112,39,19,33,16)

ecosimhist <- data.frame(histmin, histmax, freq)

center <- (histmin + histmax)/2

ecosimhist |> 
        mutate(x = (histmin + histmax)/2) |> 
        ggplot(aes(x = x, y = freq)) + 
        geom_col() + 
        scale_x_continuous(breaks = center) + 
        theme_bw()

创建于 2024-04-27,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.