Hello,
Here is some basic code to get a histogram to work with ggplot. As you wil see I have sex
and weight
in my data and then I proceed to make use of ggplot
but within it I specify a aes()
followed by geom_histogram()
I wouldn't subset the dataframe but instead use it as is. See if this helps. If not create a reprex and we can work with your data (see here: FAQ: How to do a minimal reproducible example ( reprex ) for beginners)
set.seed(1234)
df <- data.frame(
sex=factor(rep(c("F", "M"), each=200)),
weight=round(c(rnorm(200, mean=55, sd=5), rnorm(200, mean=65, sd=5)))
)
head(df)
#> sex weight
#> 1 F 49
#> 2 F 56
#> 3 F 60
#> 4 F 43
#> 5 F 57
#> 6 F 58
library(ggplot2)
# Basic histogram
ggplot(df, aes(x=weight)) + geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# Change the width of bins
ggplot(df, aes(x=weight)) +
geom_histogram(binwidth=1)
# Change colors
p<-ggplot(df, aes(x=weight)) +
geom_histogram(color="black", fill="white")
p
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Created on 2020-11-18 by the reprex package (v0.3.0)