0

I apologize if I am breaking any rules by posting this question in this forum as I am not sure wether to post this question under here or in SO. but here's my question anyway.

I am plotting a density function to get an idea regarding the spread of the data and between two categories (Yes, No). However the values are the same in these two categories (Yes/No). It is 0 everywhere and there are no other values. Here's how the dataset looks like.

df1 <- structure(list(FINAL_OUTCOME = c("Yes", "Yes", "Yes", "Yes", 
"Yes", "Yes", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No"), Value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0)), .Names = c("FINAL_OUTCOME", "Value"), row.names = 409:612, class = "data.frame")

When I plot a density function using ggplot for the values in these two categories (yes/No)

ggplot(df1, aes(x = as.numeric(Value), fill = as.factor(FINAL_OUTCOME))) + 
geom_density(alpha = 0.5) + 
scale_fill_manual( values = c("#46ACC8","#FD6467"))

what I am seeing is a plot like this below.

Two Group Density Plot

I am confused with this plot, because it shows some area for values of x < 0 and x > 0. How is this possible when all my x values are 0 ? How is it possible that that density >0 for x{-0.25 and 0} when there are no x values less than 0 or greater than 0 ?

Any suggestions or advise is much appreciated.

Glen_b
  • 257,508
  • 32
  • 553
  • 939
Science11
  • 375
  • 2
  • 12
  • 2
    The [documentation](http://ggplot2.tidyverse.org/reference/geom_density.html) for geom_density makes it clear it uses a kernel density estimate, so this boils down to "what does a kernel density estimate do?" .... – Glen_b Aug 18 '17 at 05:27
  • 2
    See [good methods for density plots of non-negative random variables in R](https://stats.stackexchange.com/questions/65866/good-methods-for-density-plots-of-non-negative-variables-in-r). See also [Why is my R density plot a bell curve when all data points are 0?](https://stats.stackexchange.com/questions/123465/why-is-my-r-density-plot-a-bell-curve-when-all-datapoints-are-0) – Glen_b Aug 18 '17 at 05:43
  • @Glen_b, your suggestions are outstanding. There were a lot of interesting comments on that thread. What worked for me is adding a parameter, `trim = TRUE `. This solved the problem. Now my density function is a straight line, exactly how i wanted it to be. I will still read those suggestions in that thread very carefully. Thanks again. – Science11 Aug 18 '17 at 06:16
  • 1
    If you feel the answers at either of those count as answers to your question please let me know so we can close as a duplicate. – Glen_b Aug 18 '17 at 07:00

0 Answers0