user8480288
user8480288

Reputation: 21

R ggplot2 adding function to a histogram

I am trying to add this function curve to a histogram. Individually, they work. But when I try to put them on the same graph, the function messes up... I can't seem to figure out how to get them together.

# make dataframe for ggplot (can use random numbers from 0 to 10 to simulate x)
c= data.frame(x= x, cx= c(seq(from= 0.001, to= 10, by= 0.001)))

x and cx have the same number of data points.

# function for curve (alpha and beta are just constants set at 0.5)
fx= function(x){
    (beta^alpha)/((x+beta)^(alpha+1)*gamma(alpha))
}

When either the geom_histogram or the stat_function is commented out, the graph works correctly.

# graph code
h_x= ggplot(data= NULL) +
    geom_histogram(data= c, aes(x= x, y= ..density..), binwidth= 0.2, col= "purple", fill= "yellow") +
    stat_function(fun= fx, data= c, aes(x= cx)) +
    coord_cartesian(xlim= c(0:10)) +
    labs(title= "Figure 03", x= "x")
plot(h_x)

Curve by itself

curve by itself;

Histogram and curve together

histogram and curve together

Upvotes: 2

Views: 850

Answers (2)

user8480288
user8480288

Reputation: 21

Thanks for the help! I ended up figuring out the problem... It's because there was some large values (greater than 100) for my x values, when I removed these points, the graph looked much better!

But now my graph looks like this:

graph without a smooth curve

n= 10000
 i= 1
 alpha= 0.5
 beta= 0.5
 x= matrix(data= 5, nrow= n)
 lambda= matrix(data= 1.5, nrow= n)

 while (i < n) {
   x[i+1]= rexp(1, rate= lambda[i])
   lambda[i+1]= (x[i+1]+beta)^(alpha+1)*(lambda[i]^alpha)*exp(-lambda[i]*(x[i+1]+beta))

if ((lambda[i+1] < 0.00001) || (lambda[i+1] > 10)) {
  while ((lambda[i+1] < 0.00001) || (lambda[i+1] > 10)) {
    x[i+1]= rexp(1, rate= lambda[i])
    lambda[i+1]= (x[i+1]+beta)^(alpha+1)*(lambda[i]^alpha)*exp(-lambda[i]*(x[i+1]+beta))
  }
}
i= i+1
}

# data frame:
df4= data.frame(x= x[x<100], cx= c(seq(from= 0.011, to= 10, by= 0.001)))  

# graph (same function (fx) from first post):
h_x= ggplot(data= df4) +
    geom_histogram(aes(x= x, y= ..density..), binwidth= 0.2, col= "purple", fill= "yellow") +
    stat_function(fun= fx) +
    coord_cartesian(xlim= c(0:10)) +
    labs(title= "Figure 03", x= "x")
  plot(h_x)

Is there any way to make it a smooth curve? I tried scale_x_continuous but to no avail...

Upvotes: 0

Marco Sandri
Marco Sandri

Reputation: 24252

Like @Gregor, I made some changes to your code and the graph looks OK.
I hope it can help you.

set.seed(1)
x <- rgamma(10000,1)
df1 <- data.frame(x= x, cx= c(seq(from= 0.001, to= 10, by= 0.001)))

beta <- alpha <- 0.5
fx <- function(x) {
    print(str(x))
    (beta^alpha)/((x+beta)^(alpha+1)*gamma(alpha))
}

# graph code
h_x <- ggplot(data=df1) +
    geom_histogram(aes(x= x, y= ..density..), binwidth= 0.2, col= "purple", fill= "yellow") +
    stat_function(fun=fx, aes(x=cx), lwd=1) +
    coord_cartesian(xlim= c(0:10)) +
    labs(title= "Figure 03", x="x")
plot(h_x)

enter image description here

Upvotes: 1

Related Questions