0

Output plot is not fitting properly into the frame while plotting hist + density plot for eda analysis in r

  hist(Absenteeism_Data$Absenteeism.time.in.hours, col = "peachpuff", border = "black", prob = TRUE, xlab = "Absenteeism.time.in.hour", main = "Absenteeism_Data")
    lines(density(Absenteeism_Data$Absenteeism.time.in.hours, na.rm = TRUE), lwd = 2, col = "chocolate3")

Here is the output graph

G5W
  • 36,531
  • 10
  • 47
  • 80
pquest
  • 303
  • 1
  • 3
  • 14
  • 2
    Welcome to SO. Please take the time to read stackoverflow.com/help/how-to-ask. It will help you craft solid questions that will hopefully get useful answers. And please...no screenshots. – orde Feb 21 '19 at 16:44
  • 1
    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Feb 21 '19 at 17:03

1 Answers1

1

Since you do not provide an example for us to work with, I demonstrate with some built-in data. I can replicate the type of problem that you are encountering using the iris data.

hist(iris$Sepal.Width, col = "peachpuff", border = "black", 
    prob = TRUE, xlab = "Sepal.Width", main = "Iris Data", breaks=5)
lines(density(iris$Sepal.Width, na.rm = TRUE), lwd = 2, col = "chocolate3")

Image with problem

The problem is that R decides the range of the y-values at the time that it make the histogram. It does not know about your planned density plot. So you must tell it to leave enough room for that. You can do that by running density first and finding the required maximum value.

DENS = density(iris$Sepal.Width, na.rm = TRUE)
YMax = max(DENS$y)
hist(iris$Sepal.Width, col = "peachpuff", border = "black", ylim=c(0,YMax),
    prob = TRUE, xlab = "Sepal.Width", main = "Iris Data", breaks=5)
lines(DENS, lwd = 2, col = "chocolate3")

Improved image

G5W
  • 36,531
  • 10
  • 47
  • 80