nicholas79171
nicholas79171

Reputation: 1273

Density graph using ggplot2

I created a craps simulator and generated some data as a result. I'm looking at the probability of wins vs losses given the number of rolls. Here's the first 25 results of my data, the rest looks exactly the same (just 50,000 rows long):

enter image description here

Here's the code I'm using to create a density graph with my data:

ggplot(df, aes(x=rollCount, fill = winOrLoss)) + 
    #geom_histogram(binwidth = 1, position = "identity", alpha=0.6) +
    scale_y_sqrt() +
    scale_x_continuous(limits=c(1, 32), breaks=1:32) +
    labs(title="Roll Count Histogram", x="Roll Count", y="Count") +
    geom_hline(aes(yintercept=0)) +
    geom_density()

and here's the resulting graph:

enter image description here

My hope is that the density graph looked something like this:

enter image description here

My main questions is how I can get it to be much more smooth instead of the up and down it currently looks like. Do I need to do something to my data before I put it in the graph? I just put it into a data frame with df <- data.frame(rollCount, winOrLoss) and let ggplot take care of the rest.

Upvotes: 1

Views: 402

Answers (1)

Roland
Roland

Reputation: 132706

You have a discrete distribution. stat_density assumes a continuous distribution. Use geom_histogram instead:

set.seed(42)
rollCount <- sample(1:20, 50, TRUE, prob = 20:1)
winOrLoss <- sample(c("W", "L"), 50, TRUE)
DF <- data.frame(rollCount, winOrLoss)

library(ggplot2)
ggplot(DF, aes(x=rollCount, fill = winOrLoss)) + 
  geom_histogram(binwidth = 1, position = "identity", alpha=0.6, 
                 aes(y = ..density..))

resulting plot

Upvotes: 1

Related Questions