ahmed2993
ahmed2993

Reputation: 35

Is there a way I can plot this density graph

I am trying to plot a density chart for two lifts, small and a large and they hold the data of the median weight of 5 people in the small lift, and median weight of 10 people in the large lift, however, I am confused on how to plot the density chart so i can compare the median weight of both the lifts

The dataset that I am using is here;

500 Person Gender-Height-Weight-Body Mass Index

Height and Weight random generated, Body Mass Index Calculated

However, i deleted the height and the index as that is irrelevant for my research

I created a two samples which are here;

  Large <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
    Small <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))

and then I put them into one dataframe called Lifts;

  Lifts<-data.frame(Large, Small)

I have tried to come up with a solution myself by plotting the following density chart;

ggplot(Lifts, aes(Large)) + geom_density(fill = "blue") + labs(x = "Median of Weight", y =     "Distribution of Data")

Any help would be appreciated

enter image description here

Upvotes: 0

Views: 56

Answers (1)

Greg
Greg

Reputation: 3670

If you'd like two groups to compare between you can use pivot_longer to make them, and then set a grouping aesthetic in your plot

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")

enter image description here

Upvotes: 2

Related Questions