问题
I have a dataframe, for example, as this:
sample1 <- seq(120,197, length.out = 60)
sample2 <- seq(113, 167, length.out = 60)
sample3 <- seq(90,180, length.out = 60)
sample4 <-seq(100, 160, length.out = 60)
df <- as.data.frame(cbind(sample1, sample2, sample3, sample4))
I now need to create histograms for these four variables such that all of them share the same y-axis, and also need to overlay normal density curves on each of these histograms. facet_wrap() will be fine as long as the y-axis is same.
Earlier today, I thought I had this issue resolved with the guidance of an expert in the forum but realised later that the solution just overlaid a density curve, not one with a normal distribution. I have tried a number options with ggplot as well as base plotting functions but what seems to be a simple task for a single variable isn't quite achievable when having multiple variables??
Any thoughts about how to go tackle this?
Thanks
回答1:
This is one possible way to do it using the tidyverse
library(tidyverse)
# example data
sample1 <- seq(120, 197, length.out = 60)
sample2 <- seq(113, 167, length.out = 60)
sample3 <- seq(90, 180, length.out = 60)
sample4 <- seq(100, 160, length.out = 60)
df <- data.frame(sample1, sample2, sample3, sample4)
# update your original dataframe to a nested dataframe by adding simulated values from normal distribution
df2 = df %>%
gather() %>% # reshape data
group_nest(key) %>% # for each key (i.e. sample)
mutate(norm = map(data, ~rnorm(10000, mean(.x$value), sd(.x$value)))) # simulate 10K observations from the corresponding normal distribution
ggplot()+
# plot histogram using info from nested column data (i.e. original observations)
geom_histogram(data = df2 %>% unnest(data), aes(value, fill=key, ..density..), alpha=0.3)+
# plot density using info from nested column norm (i.e. simulated normal observations)
geom_density(data = df2 %>% unnest(norm), aes(norm, col=key))+
# separate plots by key (i.e. sample)
facet_wrap(~key)
来源:https://stackoverflow.com/questions/56687922/normal-density-curves-on-multiple-histograms-on-a-same-plot