在同一图中绘制两个带权重的直方图。

huangapple go评论94阅读模式
英文:

Graph two weighted histograms in the same plot

问题

我试图从两个不同的数据集中绘制两个直方图,每个数据集都有一个不同的权重变量和维度。我不知道如何做,例如在ggplot2选项中,您首先必须rbind数据集,然后将直方图创建为分组变量。另外,我尝试过使用通常的hist命令,但它没有权重选项。我试图使用weights库,但我不知道如何在同一图中绘制它们。以下是我的实际代码的一部分,用于模拟数据:

install.packages("weights")
library(weights)

w1 <- seq(1, 500)
v1 <- sort(runif(500))

w2 <- seq(1, 1000)
v2 <- sort(runif(1000))

p1 <- wtd.hist(w1+1, weight=v1, density = 30, breaks= 1000, xlim=c(0, 100), col = "red")
p2 <- wtd.hist(w2, weight=v2, density = 30, breaks= 1000, xlim=c(0, 100), col = "blue")

我试图获得类似于这样的效果:

在同一图中绘制两个带权重的直方图。

英文:

I am trying to plot two histograms from two different datasets and each one has a different weight variable and dimension. I do not know how to do it, given that for example in the ggplot2 options, you have to rbind the datasets first and then create the histograms as grouped variables. Also, I tried with the usual hist command but it does not have the weight option. I was trying to use the library weights but I don´t know how to plot them in the same. This is a way of my actual code with simulate it data:

install.packages(&quot;weights&quot;)
library(weights)

w1 &lt;- seq(1,500)
v1 &lt;- sort(runif(500))

w2 &lt;- seq(1,1000)
v2 &lt;- sort(runif(1000))

p1&lt;-wtd.hist(w1+1, weight=v1, density = 30, breaks= 1000, xlim=c(0, 100), col = &quot;red&quot;)
p2&lt;-wtd.hist(w2, weight=v2, density = 30, breaks= 1000, xlim=c(0, 100), col = &quot;blue&quot;)

I am trying to get something like this:

在同一图中绘制两个带权重的直方图。

答案1

得分: 2

以下是翻译好的内容:

"你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:

The plot you illustrate is a base R plot.
这个图表是一个基本的R图。

Here's an example for plotting overlain histograms using base R which may help.
以下是使用基本的R绘制叠加直方图的示例,可能会有所帮助。

See below for response to using OP's example data with weights.
请查看下面关于使用OP的示例数据进行加权处理的回应。

set.seed(1)
hist(rnorm(500, mean = 4), 
     col = rgb(1, 0.8, 0.8, 0.5), 
     border = &quot;white&quot;,
     xlim = c(0, 10),
     xlab = &quot;Value&quot;,
     main = &quot;Overlaid histograms&quot;)
hist(rnorm(500, mean = 6), 
     col = rgb(0.6, 0.8, 1, 0.4), 
     border = &quot;white&quot;, 
     add = TRUE)
legend(&quot;topright&quot;,
       legend = c(&quot;rnorm500 mu4&quot;, &quot;rnorm500 mu6&quot;),
       fill = c(rgb(1, 0.8, 0.8, 0.5), rgb(0.6, 0.8, 1, 0.4)),
       title = &quot;Plots&quot;)

在同一图中绘制两个带权重的直方图。<!-- -->

<sup>Created on 2023-06-22 with reprex v2.0.2</sup>

Principle applied to OP's example.

library(weights)

w1 &lt;- seq(1,500)
v1 &lt;- sort(runif(500))

w2 &lt;- seq(1,1000)
v2 &lt;- sort(runif(1000))

wtd.hist(w1+1, 
         weight=v1, 
         density = 30, 
         breaks= 1000, 
         xlim=c(0, 100), 
         col = rgb(1, 0.8, 0.8, 0.5))
wtd.hist(w2, 
         weight=v2, 
         density = 30, 
         breaks= 1000, 
         xlim=c(0, 100), 
         col = rgb(0.6, 0.8, 1, 0.5),
         add = TRUE)

在同一图中绘制两个带权重的直方图。<!-- -->

<sup>Created on 2023-06-22 with reprex v2.0.2</sup>"

英文:

The plot you illustrate is a base R plot.
Here's an example for plotting overlain histograms using base R which may help.

See below for response to using OP's example data with weights.

set.seed(1)
hist(rnorm(500, mean = 4), 
     col = rgb(1, 0.8, 0.8, 0.5), 
     border = &quot;white&quot;,
     xlim = c(0, 10),
     xlab = &quot;Value&quot;,
     main = &quot;Overlaid histograms&quot;)
hist(rnorm(500, mean = 6), 
     col = rgb(0.6, 0.8, 1, 0.4), 
     border = &quot;white&quot;, 
     add = TRUE)
legend(&quot;topright&quot;,
       legend = c(&quot;rnorm500 mu4&quot;, &quot;rnorm500 mu6&quot;),
       fill = c(rgb(1, 0.8, 0.8, 0.5), rgb(0.6, 0.8, 1, 0.4)),
       title = &quot;Plots&quot;)

在同一图中绘制两个带权重的直方图。<!-- -->

<sup>Created on 2023-06-22 with reprex v2.0.2</sup>

Principle applied to OP's example.

library(weights)

w1 &lt;- seq(1,500)
v1 &lt;- sort(runif(500))

w2 &lt;- seq(1,1000)
v2 &lt;- sort(runif(1000))

wtd.hist(w1+1, 
         weight=v1, 
         density = 30, 
         breaks= 1000, 
         xlim=c(0, 100), 
         col = rgb(1, 0.8, 0.8, 0.5))
wtd.hist(w2, 
         weight=v2, 
         density = 30, 
         breaks= 1000, 
         xlim=c(0, 100), 
         col = rgb(0.6, 0.8, 1, 0.5),
         add = TRUE)

在同一图中绘制两个带权重的直方图。<!-- -->

<sup>Created on 2023-06-22 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月22日 13:58:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定