I plot a density curve using ggplot2. After I plot the data, I would like to add a normal density plot right on top of it with a fill.
Currently, I am using rnorm() to create the data but this is not efficient and would work poorly on small data sets.
library(tidyverse)
#my data that I want to plot
my.data = rnorm(1000, 3, 10)
#create the normal density plot to overlay the data
overlay.normal = rnorm(1000, 0, 5)
all = tibble(my.data = my.data, overlay.normal = overlay.normal)
all = melt(all)
ggplot(all, aes(value, fill = variable))+geom_density()
The goal would be to plot my data and overlay a normal distribution on top of it (with a fill). Something like:
ggplot(my.data)+geom_density()+add_normal_distribution(mean = 0, sd = 5, fill = "red)
Here's an approach using stat_function to define a normal curve and draw it within the ggplot call.
ggplot(my.data %>% enframe(), aes(value)) +
geom_density(fill = "mediumseagreen", alpha = 0.1) +
stat_function(fun = function(x) dnorm(x, mean = 0, sd = 5),
color = "red", linetype = "dotted", size = 1)
I figured out the solution from mixing Jon's answer and an answer from Hadley.
my.data = rnorm(1000, 3, 10)
ggplot(my.data %>% enframe(), aes(value)) +
geom_density(fill = "mediumseagreen", alpha = 0.1) +
geom_area(stat = "function", fun = function(x) dnorm(x, mean = 0, sd = 5), fill = "red", alpha = .5)
Related
I am working through a class problem to test if the central limit theorem applies to medians as well. I've written the code, and as far as I can tell, it is working just fine. But my dnorm stat to plot the normal distribution is not showing up. It just creates a flat line when it should create a bell curve. Here is the code:
set.seed(14)
median_clt <- rnorm(1000, mean = 10, sd = 2)
many_sample_medians <- function(vec, n, reps) {
rep_vec <- replicate(reps, sample(vec, n), simplify = "vector")
median_vec <- apply(rep_vec, 2, median)
return(median_vec)
}
median_clt_test <- many_sample_medians(median_clt, 500, 1000)
median_clt_test_df <- data.frame(median_clt_test)
bw_clt <- 2 * IQR(median_clt_test_df$median_clt_test) / length(median_clt_test_df$median_clt_test)^(1/3)
ggplot(median_clt_test_df, aes(x = median_clt_test)) +
geom_histogram(binwidth = bw_clt, aes(y = ..density..), fill = "hotpink1", col = "white") +
stat_function(fun = ~dnorm(.x, mean = 10, sd = 2), col = "darkorchid1", lwd = 2) +
theme_classic()
As far as I can tell, the rest of the code is working properly - it just doesn't plot the dnorm stat function correctly. The exact same stat line worked for me before, so I'm not sure what's gone wrong.
The line isn't quite flat; it's just very stretched out compared to the histogram. We can see this more clearly if we zoom out on the x axis and zoom in on the y axis:
ggplot(median_clt_test_df, aes(x = median_clt_test)) +
geom_histogram(binwidth = bw_clt, aes(y = ..density..),
fill = "hotpink1", col = "white") +
stat_function(fun = ~dnorm(.x, mean = 10, sd = 2),
col = "darkorchid1",
lwd = 2) +
xlim(c(5, 15)) +
coord_cartesian(xlim = c(5, 15), ylim = c(0, 1)) +
theme_classic()
But why is this?
It's because you are using dnorm to plot the distribution of the random variable from which the medians were drawn, but your histogram is a sample of the medians themselves. So you are plotting the wrong dnorm curve. The sd should not be the standard deviation of the random variable, but the standard deviation of the sample medians:
ggplot(median_clt_test_df, aes(x = median_clt_test)) +
geom_histogram(binwidth = bw_clt, aes(y = ..density..),
fill = "hotpink1", col = "white") +
stat_function(fun = ~dnorm(.x,
mean = mean(median_clt_test),
sd = sd(median_clt_test)),
col = "darkorchid1",
lwd = 2)
theme_classic()
If you prefer you could use the theoretical standard error of the mean instead of the measured standard deviation of your medians - these will be very similar.
# Theoretical SEM
2/sqrt(500)
#> [1] 0.08944272
# SD of medians
sd(median_clt_test)
#> [1] 0.08850221
In a data.frame, I would like to be able to compare the density estimates by ggplot2::geom_violin() with the ones that would be computed with stat_function() and this for every factor.
In this settting, I want to compare the empirical density of 2 samples of size 100 with the true density of normal distributions with mean 10 and 20.
library(tidyverse)
test <- tibble(a = rnorm(100, mean = 10),
b = rnorm(100, mean = 20)) %>%
gather(key, value)
One way to achieve this is to replicate for every factor an overlay of stat_density and stat_function. However for too many factors this would create too many plots. (multiple answers on these questions exist : e.g. overlay histogram with empirical density and dnorm function)
For the clarity of the next graphs i use the geom_flat_violin of #DavidRobinson : dgrtwo/
geom_flat_violin.R.
source("geom_flat_violin.R")
# without the "true" distribution
test %>%
ggplot(aes(x = key, y = value)) +
geom_flat_violin(col = "red", fill = "red", alpha = 0.3) +
geom_point()
# comparing with the "true" distribution
test %>%
ggplot(aes(x = key, y = value)) +
geom_flat_violin(col = "red", fill = "red", alpha = 0.3) +
geom_point() +
geom_flat_violin(data = tibble(value = rnorm(10000, mean = 10), key = "a"),
fill = "blue", alpha = 0.2)
The problem with this solution is that it requires to simulate for every factor enough simulated data points so that the final density is smooth enough. For the normal distribution 10000 is enough but for other distributions it might be necessary to simulate even more points.
The question is : can the stat_functions be used to achieve this so that it is not mandatory to simulate data?
stat_function(fun = dnorm, args = list(mean = 10))
stat_function(fun = dnorm, args = list(mean = 20))
Rather than having to calculate the density of a large sample, you could simply get the distribution directly and plot it as a polygon:
library(tidyverse)
test <- tibble(a = rnorm(100, mean = 10),
b = rnorm(100, mean = 20)) %>%
gather(key, value)
test %>%
ggplot(aes(x = key, y = value)) +
geom_flat_violin(col = "red", fill = "red", alpha = 0.3) +
geom_point() +
geom_polygon(data = tibble(value = seq(7, 13, length.out = 100),
key = 1 + dnorm(value, 10)),
fill = "blue", colour = "blue", alpha = 0.2)
I have created a graph that overlays a normally distributed density plot on top of a previous density plot using the dnorm() function. However, I am having a difficult time adding a legend. Below is the code to create the plot with one of my attempts at adding a legend.
library(tidyverse)
my.data = rnorm(1000, 3, 10)
ggplot(enframe(my.data), aes(value)) +
geom_density(fill = "mediumseagreen", alpha = 0.1) +
geom_area(stat = "function", fun = function(x) dnorm(x, mean = 0, sd = 5), fill = "red", alpha = .5)+
theme(legend.position="right")+
scale_color_manual("Line.Color", values=c(red="red",green="green"),
labels=paste0("Plot",1:2))
To summarize I am trying to add a legend to this plot that has labels "Plot1" and "Plot2"
There might be better answers. This is what I have achieved with several attemps:
library(tidyverse)
my.data = rnorm(1000, 3, 10)
ggplot(enframe(my.data), aes(value)) +
geom_density(aes(color = "Plot1", fill = "Plot1"), alpha = 0.1) +
geom_area(aes(color = "Plot2", fill = "Plot2"), stat = "function",
fun = function(x) dnorm(x, mean = 0, sd = 5), alpha = .5)+
theme(legend.position="right") +
scale_color_manual(" ", values=c(Plot1="green", Plot2="red")) +
scale_fill_manual(" ", values=c(Plot1 ="green", Plot2="red"))
I have two data sets, their size is 500 and 1000. I want to plot density for these two data sets in one plot.
I have done some search in google.
r-geom-density-values-in-y-axis
ggplot2-plotting-two-or-more-overlapping-density-plots-on-the-same-graph/
the data sets in above threads are the same
df <- data.frame(x = rnorm(1000, 0, 1), y = rnorm(1000, 0, 2), z = rnorm(1000, 2, 1.5))
But if I have different data size, I should normalize the data first in order to compare the density between data sets.
Is it possible to make density plot with different data size in ggplot2?
By default, all densities are scaled to unit area. If you have two datasets with different amounts of data, you can plot them together like so:
df1 <- data.frame(x = rnorm(1000, 0, 2))
df2 <- data.frame(y = rnorm(500, 1, 1))
ggplot() +
geom_density(data = df1, aes(x = x),
fill = "#E69F00", color = "black", alpha = 0.7) +
geom_density(data = df2, aes(x = y),
fill = "#56B4E9", color = "black", alpha = 0.7)
However, from your latest comment, I take that that's not what you want. Instead, you want the areas under the density curves to be scaled relative to the amount of data in each group. You can do that with the ..count.. aesthetics:
df1 <- data.frame(x = rnorm(1000, 0, 2), label=rep('df1', 1000))
df2 <- data.frame(x = rnorm(500, 1, 1), label=rep('df2', 500))
df=rbind(df1, df2)
ggplot(df, aes(x, y=..count.., fill=label)) +
geom_density(color = "black", alpha = 0.7) +
scale_fill_manual(values = c("#E69F00", "#56B4E9"))
I'm trying to show data of two groups. I am using the ggplot2 package to graph the data and using stat_summary() to obtain a point estimate (mean) and 90% CI within the plot of the data. What I'd like is for the mean and confidence interval be structured off to the right of the points representing the distribution of the data. Currently, stat_summary() will simply impose the mean and CI over top of the distribution.
Here is an example of data that I am working with:
set.seed(9909)
Subjects <- 1:100
values <- c(rnorm(n = 50, mean = 30, sd = 5), rnorm(n = 50, mean = 35, sd = 8))
data <- cbind(Subjects, values)
group1 <- rep("group1", 50)
group2 <- rep("group2", 50)
group <- c(group1, group2)
data <- data.frame(data, group)
data
And this is what my current ggplot2 code looks like (distribution as points with the mean and 90% CI overlaid on top for each group):
ggplot(data, aes(x = group, y = values, group = 1)) +
geom_point() +
stat_summary(fun.y = "mean", color = "red", size = 5, geom = "point") +
stat_summary(fun.data = "mean_cl_normal", color = "red", size = 2, geom = "errorbar", width = 0, fun.args = list(conf.int = 0.9)) + theme_bw()
Is it possible to get the mean and confidence intervals to position_dodge to the right of their respective groups?
You can use position_nudge:
ggplot(data, aes(x = group, y = values, group = 1)) +
geom_point() +
stat_summary(fun.y = "mean", color = "red", size = 5, geom = "point",
position=position_nudge(x = 0.1, y = 0)) +
stat_summary(fun.data = "mean_cl_normal", color = "red", size = 2,
geom = "errorbar", width = 0, fun.args = list(conf.int = 0.9),
position=position_nudge(x = 0.1, y = 0)) +
theme_bw()