Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain grouping information for groups with missing values #4908

Closed
GregorDall opened this issue Jul 14, 2022 · 2 comments
Closed

Retain grouping information for groups with missing values #4908

GregorDall opened this issue Jul 14, 2022 · 2 comments

Comments

@GregorDall
Copy link

I am coming here through an issue I encountered in ggpairs. I have missing data for one of my groups so some panels contain information about a different number of groups. I think this is a problem inherited from ggplot2, because it removes NA values from the data.

In this example I have colors for three groups.

data(iris)
ggplot(iris, aes(Petal.Length, fill = Species))  + geom_histogram()

grafik

If I remove data for one group, the colors are chosen differently, but I would like to retain the grouping information and therefore the colors provided, without manually setting colors using scale_fill_manual(). It also throws a warning. Is there a parameter which I could set that retains the groups with missing values (e.g. drop=FALSE)?

iris[iris$Species=="setosa","Petal.Length"] <- NA
ggplot(iris, aes(Petal.Length, fill = Species))  + geom_histogram()

Warning message:
Removed 50 rows containing non-finite values (stat_bin).

grafik

This is the ggpairs report:
ggobi/ggally#445 (comment)

@yutannihilation
Copy link
Member

drop=FALSE

Yes, you can specify it in the fill scale.

library(ggplot2)

iris[iris$Species=="setosa","Petal.Length"] <- NA
ggplot(iris, aes(Petal.Length, fill = Species)) +
  geom_histogram() +
  scale_fill_discrete(drop = FALSE)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 50 rows containing non-finite values (`stat_bin()`).

Created on 2022-07-14 by the reprex package (v2.0.1)

You can also use factor to avoid warnings.

library(ggplot2)

iris$Species <- factor(iris$Species)

iris <- iris[iris$Species != "setosa", ]

ggplot(iris, aes(Petal.Length, fill = Species)) +
  geom_histogram() +
  scale_fill_discrete(drop = FALSE)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2022-07-14 by the reprex package (v2.0.1)

@GregorDall
Copy link
Author

Thank you @yutannihilation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants