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

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

Graph two weighted histograms in the same plot

问题

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

  1. install.packages("weights")
  2. library(weights)
  3. w1 <- seq(1, 500)
  4. v1 <- sort(runif(500))
  5. w2 <- seq(1, 1000)
  6. v2 <- sort(runif(1000))
  7. p1 <- wtd.hist(w1+1, weight=v1, density = 30, breaks= 1000, xlim=c(0, 100), col = "red")
  8. 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:

  1. install.packages(&quot;weights&quot;)
  2. library(weights)
  3. w1 &lt;- seq(1,500)
  4. v1 &lt;- sort(runif(500))
  5. w2 &lt;- seq(1,1000)
  6. v2 &lt;- sort(runif(1000))
  7. p1&lt;-wtd.hist(w1+1, weight=v1, density = 30, breaks= 1000, xlim=c(0, 100), col = &quot;red&quot;)
  8. 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的示例数据进行加权处理的回应。

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

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

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

Principle applied to OP's example.

  1. library(weights)
  2. w1 &lt;- seq(1,500)
  3. v1 &lt;- sort(runif(500))
  4. w2 &lt;- seq(1,1000)
  5. v2 &lt;- sort(runif(1000))
  6. wtd.hist(w1+1,
  7. weight=v1,
  8. density = 30,
  9. breaks= 1000,
  10. xlim=c(0, 100),
  11. col = rgb(1, 0.8, 0.8, 0.5))
  12. wtd.hist(w2,
  13. weight=v2,
  14. density = 30,
  15. breaks= 1000,
  16. xlim=c(0, 100),
  17. col = rgb(0.6, 0.8, 1, 0.5),
  18. 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.

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

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

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

Principle applied to OP's example.

  1. library(weights)
  2. w1 &lt;- seq(1,500)
  3. v1 &lt;- sort(runif(500))
  4. w2 &lt;- seq(1,1000)
  5. v2 &lt;- sort(runif(1000))
  6. wtd.hist(w1+1,
  7. weight=v1,
  8. density = 30,
  9. breaks= 1000,
  10. xlim=c(0, 100),
  11. col = rgb(1, 0.8, 0.8, 0.5))
  12. wtd.hist(w2,
  13. weight=v2,
  14. density = 30,
  15. breaks= 1000,
  16. xlim=c(0, 100),
  17. col = rgb(0.6, 0.8, 1, 0.5),
  18. 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:

确定