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):
- How can I make the warning go away?
- Can I ignore the warning?
- Is there something wrong with this practice?
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: