2

I try to plot a weighted density with ggplot2. The results seem to be fine, but I get the following warning: Warning: Ignoring unknown aesthetics: weight. Similar problems seem to appear in other ggplot2 applications and therefore I am wondering, if the warning could be ignored.

Reproducible example:

library(ggplot2)

set.seed(123)

# Some random data & weights
x <- rnorm(1000, 5)
w <- x^5

# Plot unweighted
ggplot() + stat_density(aes(x = x))

# Plot weighted - Warning: Ignoring unknown aesthetics: weight
ggplot() + stat_density(aes(x = x, weight = w / sum(w))) # Weighting seems to work fine

# Comparison of weighted density in base graphics - Same results as with ggplot2
plot(density(x, weights = w / sum(w)))

Can this warning message be ignored?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Joachim Schork
  • 2,025
  • 3
  • 25
  • 48
  • You could try `options(warn=-1)`? ([source](https://stackoverflow.com/a/45178787/3526832)) – h3rm4n Dec 01 '17 at 11:20
  • @h3rm4n You should never do that. Use `suppressWarnings` if needed. Anyway, this should be submitted to the ggplot2 bug tracker (if it hasn't been submitted yet). – Roland Dec 01 '17 at 11:22
  • @h3rm4n I do not care that the warning is shown, I just want to make sure that my results are correct. – Joachim Schork Dec 01 '17 at 11:24
  • @Roland What is the "ggplot2 bug tracker" and where can I find it? Would you agree that my results are valid, even though I receive a warning message? – Joachim Schork Dec 01 '17 at 11:25
  • @Roland Why is that? For the reason as mentioned in the linked source? – h3rm4n Dec 01 '17 at 11:26
  • The reason you are getting this warning is that ggplot2 doesn't have a `weight` aestetic AFAIK (which is what the warning message is trying to tell you) – h3rm4n Dec 01 '17 at 11:27
  • @JoachimSchork You can find the link at the [ggplot2 CRAN page](https://cran.r-project.org/web/packages/ggplot2/index.html). I give no warranties for correctness, even for my own packages. – Roland Dec 01 '17 at 11:27

3 Answers3

2

You can avoid the warning by using geom_density:

ggplot() + 
  geom_density(aes(x = x, weight = w / sum(w)), color = "green") +
  geom_density(aes(x = x), color = "blue")

resulting plot

I would have expected the stat_ function to handle the same aesthetics as the geom and it appears to do so. The warning would then be a bug that should be reported to the maintainers.

Roland
  • 127,288
  • 10
  • 191
  • 288
2

Whether or not your get a warning appears to depend on where you give the weight argument (ggplot2 version 2.2.1):

Following these answers: Create weighted histogram, Histogram with weights

Setup data:

w = seq(1,1000)
v = sort(runif(1000))
foo = data.frame(v,w)

The following command produces a warning:

ggplot(foo) + geom_histogram(aes(v, weight=w),bins = 30)

These commands do not produce a warning:

ggplot(foo, aes(v, weight=w)) + geom_histogram(bins = 30)
ggplot(foo, aes(weight=w)) + geom_histogram(aes(v),bins = 30)

But all three commands produce the same plot.

Simon.S.A.
  • 6,240
  • 7
  • 22
  • 41
1

Here is another solution:

ggplot(data=NULL, aes(x = x, weight=w/sum(w))) + stat_density() 

And:

ggplot(data=NULL, aes(x = x, weight=w/sum(w))) + 
   stat_density(fill=NA, color = "green") + 
   stat_density(aes(x=x), fill=NA, color = "blue", inherit.aes=F)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58