Reputation: 11
I'm trying to adjust a plot of the beta(1/2, 1/2) distribution to fit into the vertical bars between x=.05 and 1.0. The code below gets very close.
library(ggplot2)
# Set the shape parameters
shape1 <- .5
shape2 <- .5
# Set the lower and upper limits
lower <- 0.05
upper <- 0.99888
# Calculate the probability density function (PDF) at every point between 0.10 and 1
df <- data.frame(x = seq(lower, upper, length.out = 1000), y = dbeta(seq(lower, upper, length.out = 1000), shape1, shape2))
# Plot the PDF with custom tick marks and labels on the x-axis, and a tick mark at (0,0)
ggplot(df, aes(x = x, y = y)) +
geom_line() +
xlab(expression(kappa[j])) + ylab("") +
scale_x_continuous(limits = c(0, 1), expand = c(0, 0),
breaks = seq(0, 1, 0.5), labels = c("0.0", "0.5", "1.0")) +
theme_classic() +
theme(axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank()) +
#geom_vline(xintercept = 0, color = "black") +
geom_vline(xintercept = 0.05, color = "black") +
geom_vline(xintercept = 1, color = "black") +
labs(title = "")
Here is the plot that is generated by this code.
I need the plot to fit between x=.05 and x=1 and maintain the U shape of the distribution. I realize that this will make it a bit off center from 0.5, but that's the point.
Thanks in advance,
David
Upvotes: 0
Views: 260
Reputation: 2506
As mentioned in my comment, this is artificially removing instances of the beta distribution that are below 0.05.
df <- data.frame(x = rbeta(n = 100000, 0.5, 0.7))
df %>%
filter(x > 0.05) %>%
ggplot(aes(x = x)) +
geom_histogram(bins = 100) +
geom_vline(xintercept = 0.05, color = "black", linetype = "longdash") +
geom_vline(xintercept = 1, color = "black", linetype = "longdash")
Could be converted into a line graph if needed, but this was easier to generate quickly.
Upvotes: 0