How to Avoid Density Curve Getting Cut Off in Plot

How To Avoid Density Curve Getting Cut Off In Plot

Here's the simple literal answer to the question. Make an object to hold the result of your density call and use that to set the ylim of the histogram.

x <- rexp(1000, 0.2)
tmp <- density(x)
hist(x, prob = TRUE, ylim = c(0, max(tmp$y)))
lines(tmp, col = "blue", lty = 3, lwd = 2)

(should probably go to SO)

cut off density plot in ggplot2

According to the ggplot2 book, you use scale_x_continuous(limits=c(1,20)) instead of xlim(1,20) for that.

Line at the top of a ridgeline density plot is cut off

Adding

scale_y_discrete(expand = c(0.01, 0))

did the trick.

R: calculate area under a density curve until a cutoff value

Create an empirical cumulative distribution function:

q <- quantile(df[df$group=="a", "x"], probs = 0.05)
ecdf(df[df$group=="b", "x"])(q)
#[1] 0.255

Finding cutoff between two overlapping density plots

Here is an answer based on the link provided by @Julius:

A.density <- density(subset(df, genotype == "A")$value, from = min(df$value), to = max(df$value), n = 2^10)
B.density <- density(subset(df, genotype == "B")$value, from = min(df$value), to = max(df$value), n = 2^10)
intersection.point <- A.density$x[which(diff((A.density$y - B.density$y) > 0) != 0) + 1]

Bar label is getting cut from the plot and mai/omi parameters aren't fixing the issue

You can change the y axis limits with the argument ylim

hist(airquality$Ozone,
col = "lightblue4", main="",ylim = c(0,40) ,
labels = TRUE,yaxt='n',ylab = "", xlab="Ozone (ppb)")

Density plot cut off and remove additional line in ggplot2

I just found the response, I used stat_density() instead of geom_density() and add arguments geom="line" and position="identity".



Related Topics



Leave a reply



Submit