Perhaps the answer is to just be warned. I am attmepting to use a scaled and centered variable to look at how observations differ from the mean value. This plot is a common practice. But when I do this I get a warning from ggplot2
.
Warning messages:
1: Stacking not well defined when ymin != 0
I like to have ggplot2 and the rest of the world happy and no warnings coming my way. I tried to get rid of the warning in the following ways and searched SO (see links at bottom for some more promising questions) for related questions. Still my friend ggplot2 is warning me.
QUESTION(S):
Code attempts:
## The data
mtcars$scaled_mpg <- unlist(tapply(mtcars$mpg, mtcars$cyl, scale))
mtcars <- mtcars[order(mtcars$cyl), ]
mtcars$ID <- unlist(tapply(mtcars$cyl, mtcars$cyl, seq_along))
mtcars$ID <- factor(sprintf("%02d", mtcars$ID ))
## ================ Attempt 1 ================
ggplot(mtcars, aes(x = ID, y = scaled_mpg, fill = factor(cyl))) +
geom_bar(stat="identity") + facet_grid(cyl~.)
## ================ Attempt 2 ================
ggplot(mtcars, aes(x = ID, fill = factor(cyl))) +
geom_bar(aes(weight = scaled_mpg)) + facet_grid(cyl~.)
## ================ Attempt 3 ================
dat1 <- subset(mtcars, scaled_mpg >= 0)
dat2 <- subset(mtcars, scaled_mpg < 0)
ggplot() +
geom_bar(data = dat1, aes(x = ID, y = scaled_mpg,
fill = factor(cyl)),stat = "identity") +
geom_bar(data = dat2, aes(x = ID, y = scaled_mpg,
fill= factor(cyl)),stat = "identity") +
facet_grid(cyl~.)
The plot:
Similar posts:
1) Either by adding position = "identity"
to geom_bar
or, of course, by using
suppressWarnings(print(ggplot(...)))
2-3) Considering the technical side - yes, you can ignore it. The reason for this warning is related to interpreting that bars have negative height instead of just negative values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With