我正在尝试为两个电梯绘制一个密度图,小电梯和大电梯,它们保存着小电梯中5个人的中位数体重和大型电梯中10个人的中位数体重的数据,但是,我不知道如何绘制密度图,以便我可以比较两个电梯的中位数重量
我使用的数据集是here;
500人性别-身高-体重-体重指数
随机生成身高和体重,计算体重指数
但是,我删除了高度和索引,因为这与我的研究无关
我创建了两个示例,这两个示例在这里;
Large <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
Small <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))然后我把它们放入一个叫做Lifts的数据框中;
Lifts<-data.frame(Large, Small)我试着自己想出一个解决方案,画出下面的密度图;
ggplot(Lifts, aes(Large)) + geom_density(fill = "blue") + labs(x = "Median of Weight", y = "Distribution of Data")任何帮助都将不胜感激

发布于 2020-02-19 05:59:55
如果你想在两组之间进行比较,你可以使用pivot_longer来制作它们,然后在你的图中设置一个分组美学
library(ggplot)
library(tidyr)
Large <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
Small <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
Lifts<-data.frame(Large, Small)
Lifts_long <- pivot_longer(Lifts, cols = c(Small, Large), names_to = "name", values_to = "value")
ggplot(Lifts_long, aes(value)) +
geom_density(aes(group = name, fill = name), alpha = 0.5) +
labs(x = "Median of Weight",
y = "Distribution of Data",
fill = "Group Name")

https://stackoverflow.com/questions/60289869
复制相似问题