Error in stat_function for dnbinom

272 views
Skip to first unread message

Timothy Lau

unread,
Mar 22, 2017, 12:25:06 PM3/22/17
to ggplot2
If I  have a histogram

x <- data.frame(x = rnorm(n = 100, mean = 1, sd = 1))
ggplot(data = x, mapping = aes(x = x)) + geom_histogram(binwidth = 0.5)

and I want to overlay a density plot I can use:

ggplot(data = x, mapping = aes(x = x)) + 
  geom_histogram(mapping = aes(y = ..density..), binwidth = 0.5) + 
  stat_function(fun = dnorm, args = list(mean = mean(x$x), sd = sd(x$x)), color = "black") +
  geom_vline(mapping = aes(xintercept = mean(x = x, na.rm = T)), color = "red", linetype = "dashed", size = 1)

​But this method undesirably changes my ​y-axis to the density. So I found that I can use:

​ggplot(data = x, aes(x = x)) +
  stat_bin(binwidth = 0.5, color = "black", fill = NA) +
  stat_function(fun = dnorm.count, args = list(mean = mean(x$x), sd = sd(x$x), n = nrow(x), binwidth = 0.5), color = "red") +
  geom_vline(mapping = aes(xintercept = mean(x = x, na.rm = T)), color = "red", linetype = "dashed", size = 1) +
  stat_bin(binwidth = 0.5, geom = "text", mapping = aes(label = ..count..), vjust = -0.5)

Which is okay, but when I try to do the same thing for a negative binomial distribution (instead of Gaussian as before):

dnbinom.count <- function(x, mu , size, log = FALSE, n = 1, binwidth = 1){
  n * binwidth * dnorm(x = x, mu = mu, size = size, log = log) 
}
y <- data.frame(y = rnbinom(n = 1000, mu = 5, size = 8))
ggplot(data = y, aes(x = y)) +
  stat_bin(binwidth = 1, color = "black", fill = NA) +
  stat_function(fun = dnbinom.count, args = list(mu = mean(y$y), size = sd(y$y)^2, n = nrow(y), binwidth = 1), color = "red") +
  geom_vline(mapping = aes(xintercept = mean(x = y, na.rm = T)), color = "red", linetype = "dashed", size = 1) +
  stat_bin(binwidth = 1, geom = "text", mapping = aes(label = ..count..), vjust = -0.5)


 I get the following error​:

Warning message:
Computation failed in `stat_function()`:
unused arguments (mu = mu, size = size) 



Can someone help me with this error? Or possibly suggest a simpler suggestion to viewing a density plot overlaid on my histogram that preserves my count y-axis.




Best,
Timothy



"The will to win means nothing without the will to prepare."

Loveland, Adam

unread,
Mar 22, 2017, 1:25:21 PM3/22/17
to Timothy Lau, ggplot2

Shouldn’t your dnbinom.count function instead call dnbinom (and not dnorm)?

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may be considered a commercial electronic message under Canadian law or this message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US law. You may unsubscribe at any time from receiving commercial electronic messages from PNC at http://pages.e.pnc.com/globalunsub/
PNC, 249 Fifth Avenue, Pittsburgh, PA 15222; pnc.com

Reply all
Reply to author
Forward
0 new messages