# How to develop the following graph in ggplot2?

How to develop the following graph in ggplot2?

``````#Find local minimum in bimodal distribution with r
simulate <- function(lambda=0.3, mu=c(0, 4), sd=c(1, 1), n.obs=10^5) {
x1 <- rnorm(n.obs, mu[1], sd[1])
x2 <- rnorm(n.obs, mu[2], sd[2])
return(ifelse(runif(n.obs) < lambda, x1, x2))
}
data <- simulate()
optimize(approxfun(d\$x,d\$y),interval=c(0,4))\$minimum
hist(data,prob=TRUE)
lines(d, col="red", lty=2)
v <- optimize(approxfun(d\$x,d\$y),interval=c(1,4))\$minimum
abline(v=v, col="blue")
``````

You can use geom_histogram() function to create histogram, geom_line() for line and geom_vline for the vertical blue line.

ggplot(data=tablename, aes(x=x-axisname, y=y-axisname)) +
geom_histogram(color="black", fill=#bab5a6)+
geom_line(linetype = "dashed", color = "red")+
geom_vline(xintercept=c(-2,2(specify according to your need)),size=1,color = "blue")

Hopefully this will work , please let me know if it didn't worked for you

Script corrected.
the line was missing:
d <- density (data)
My apologies!

``````#Find local minimum in bimodal distribution with r
simulate <- function(lambda=0.3, mu=c(0, 4), sd=c(1, 1), n.obs=10^5) {
x1 <- rnorm(n.obs, mu[1], sd[1])
x2 <- rnorm(n.obs, mu[2], sd[2])
return(ifelse(runif(n.obs) < lambda, x1, x2))
}
data <- simulate()
d <- density(data)
optimize(approxfun(d\$x,d\$y),interval=c(0,4))\$minimum
hist(data,prob=TRUE)
lines(d, col="red", lty=2)
v <- optimize(approxfun(d\$x,d\$y),interval=c(1,4))\$minimum
abline(v=v, col="blue")
``````
``````v <- optimize(approxfun(d\$x,d\$y),interval=c(1,4))\$minimum
abline(v=v, col="blue")
point<-c(v,d\$y[v])
ddf <- data.frame(x=d\$x,y=d\$y)
ggplot(data=ddf, aes(x=x, y=y)) +
geom_histogram(color="black", fill="grey50")+
geom_line(linetype = "dashed", color = "red")+
geom_vline(xintercept=point,size=1,color = "blue")
``````
``````simulate <- function(lambda=0.3, mu=c(0, 4), sd=c(1, 1), n.obs=10^5) {
x1 <- rnorm(n.obs, mu[1], sd[1])
x2 <- rnorm(n.obs, mu[2], sd[2])
return(ifelse(runif(n.obs) < lambda, x1, x2))
}
data <- simulate()
d <- density(data)
v <- optimize(approxfun(d\$x,d\$y),interval=c(1,4))\$minimum

library(tidyverse)

hd <- hist(data,plot=FALSE,right=TRUE)
(hd_df <- tibble(
counts=hd\$counts,
density=hd\$density,
mids=hd\$mids
))

(rel_range <-range(hd\$counts)/range(hd\$density))
(dens_to_c <- rel_range[1])

ggplot(data=hd_df,mapping = aes(x=mids)) +
geom_col(mapping=aes(y=counts),
color="black",fill="gray")+
geom_line(mapping=aes(y=density*dens_to_c),
linetype = "dashed", color = "red")+
scale_y_continuous(sec.axis = sec_axis(~./dens_to_c)) +
geom_vline(xintercept=v,size=1,color = "blue")
``````

on second thoughts this seems to work better

``````ggplot(data=hd_df,mapping = aes(x=mids)) +
geom_col(mapping=aes(y=counts),
color="black",fill="gray")+
geom_line(data=data.frame(dx=d\$x,dy=d\$y),
mapping=aes(x=dx,y=dy*dens_to_c),
linetype = "dashed", color = "red")+
scale_y_continuous(sec.axis = sec_axis(~./dens_to_c)) +
geom_vline(xintercept=v,size=1,color = "blue")
``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.