英文:
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("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 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 = "white",
xlim = c(0, 10),
xlab = "Value",
main = "Overlaid histograms")
hist(rnorm(500, mean = 6),
col = rgb(0.6, 0.8, 1, 0.4),
border = "white",
add = TRUE)
legend("topright",
legend = c("rnorm500 mu4", "rnorm500 mu6"),
fill = c(rgb(1, 0.8, 0.8, 0.5), rgb(0.6, 0.8, 1, 0.4)),
title = "Plots")
<!-- -->
<sup>Created on 2023-06-22 with reprex v2.0.2</sup>
Principle applied to OP's example.
library(weights)
w1 <- seq(1,500)
v1 <- sort(runif(500))
w2 <- seq(1,1000)
v2 <- 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 = "white",
xlim = c(0, 10),
xlab = "Value",
main = "Overlaid histograms")
hist(rnorm(500, mean = 6),
col = rgb(0.6, 0.8, 1, 0.4),
border = "white",
add = TRUE)
legend("topright",
legend = c("rnorm500 mu4", "rnorm500 mu6"),
fill = c(rgb(1, 0.8, 0.8, 0.5), rgb(0.6, 0.8, 1, 0.4)),
title = "Plots")
<!-- -->
<sup>Created on 2023-06-22 with reprex v2.0.2</sup>
Principle applied to OP's example.
library(weights)
w1 <- seq(1,500)
v1 <- sort(runif(500))
w2 <- seq(1,1000)
v2 <- 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论