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

inherit mapping in geom_miss_point #137

Closed
jminnier opened this issue Feb 19, 2018 · 5 comments · Fixed by #144
Closed

inherit mapping in geom_miss_point #137

jminnier opened this issue Feb 19, 2018 · 5 comments · Fixed by #144

Comments

@jminnier
Copy link

Hey there, I love your package and especially the geom_miss_point function, but I think I found some unexpected behavior. My issue is that I cannot seem to get geom_miss_point to inherit the fill or shape in the aes(). Here's a reprex showing what I mean:

library(ggplot2)
library(naniar)

mydat <- mtcars
mydat$mpg[1:10] = NA

Here is a simple missing plot that I'm using as a basis:

ggplot(mydat,aes(x=mpg,y=disp))+geom_point()+geom_miss_point()
#> Warning: Removed 10 rows containing missing values (geom_point).

Example showing it works for regular geom_point: fill and color are inherited with geom_point just fine and this is what I'd like to emulate in geom_miss_point.

ggplot(mtcars,aes(x=mpg,y=disp,fill=cyl))+
  geom_point(size=4,alpha=0.5,pch=21)+
  geom_point(aes(x=mpg+2),pch=21)

ggplot(mtcars,aes(x=mpg,y=disp,color=cyl))+
  geom_point(size=4,alpha=0.5)+
  geom_point(aes(x=mpg+2))

However, fill is not inherited with geom_miss_point. I'd like to have the missing points filled with their respective values of cyl:

ggplot(mydat,aes(x=mpg,y=disp,fill=cyl))+
  geom_point(pch=21,size=4)+
  #geom_point(pch=21,aes(x=mpg+2))+
  geom_miss_point(pch=21,size=4)
#> Warning: Removed 10 rows containing missing values (geom_point).

also, trying to inherit size gives an error:

ggplot(mydat,aes(x=mpg,y=disp,size=cyl))+
  geom_point(pch=21)+
  geom_miss_point(pch=21)
#> Warning: Removed 10 rows containing missing values (geom_point).
#> Error in rep(value[[k]], length.out = n): attempt to replicate an object of type 'symbol'

Specifying fill in the function does not work, either

ggplot(mydat,aes(x=mpg,y=disp,fill=cyl))+geom_point(pch=21,size=4)+
  geom_miss_point(aes(fill=cyl),pch=21,size=4)
#> Warning: Removed 10 rows containing missing values (geom_point).

But specifying fill as a constant does work.

ggplot(mydat,aes(x=mpg,y=disp,fill=cyl))+geom_point(pch=21,size=4)+
  geom_miss_point(fill="red",pch=21,size=4)
#> Warning: Removed 10 rows containing missing values (geom_point).

I tried forking your repo and playing around with geom_miss_point and GeomMissPoint but I couldn't figure out how to fix it. I'm not very experienced with extending ggplot2 and I think this is beyond my knowledge. I also tried updating ggplot2 to the development version but that didn't make a difference. Wish I could have been more helpful in figuring this out! I will keep trying, but perhaps if you get a chance to check it out it will be easy for you to see what's causing this behavior?

Thanks for naniar, it truly is absurdly useful!

@seasmith
Copy link
Contributor

seasmith commented Feb 19, 2018

Using "miss_point" within geom_point() gives:

ggplot(mydat) +
    geom_point(aes(x=mpg,y=disp, color = ..missing.., fill= cyl),
               pch = 21, size = 4, stat = "miss_point")

So I made a slight mod to StatMissPoint:

ggplot(mydat) +
    geom_point(aes(x=mpg,y=disp, color = ..missing.., fill= cyl),
               pch = 21, size = 4, color = "black",stat = "miss_point")

The mod I made in StatMissPoint was to compute_group:

data.frame(data,
           missing = missing_label)

However, I did notice this behavior:

# Works using the unmodified `StatMissPoint`
ggplot(mydat, aes(x = mpg, y = disp, fill = factor(cyl)))+
    geom_point(pch = 21, size = 4)+
    stat_miss_point(pch = 21, size = 4)

# Throws the same error in both cases of `StatMissPoint`
ggplot(mydat, aes(x = mpg, y = disp, fill = factor(cyl)))+
    geom_point(pch = 21, size = 4)+
    geom_miss_point(pch = 21, size = 4)

#> Error in rep(value[[k]], length.out = n) : 
#>  attempt to replicate an object of type 'symbol'
#> In addition: Warning message:
#> Removed 10 rows containing missing values (geom_point). 

@njtierney
Copy link
Owner

Hi!

Thanks for filing the complement, and the bug report!

Ideally geom_miss_point() should behave as geom_point .

@seasmith - can you point to the changes you made in StatMissingPoint ?

This should be fixed in the next release.

@njtierney
Copy link
Owner

Also, thank you to both of you @jminnier and @seasmith for trying to work out how to implement this - really appreciate it! :)

njtierney added a commit that referenced this issue Apr 17, 2018
Correct 'fill' aesthetic behavior in geom_miss_point() #137
@njtierney
Copy link
Owner

Thanks to the kind work of @seasmith I think that this works now:

library(ggplot2)
library(naniar)

ggplot(airquality,
       aes(x = Ozone,
           y = Solar.R,
           colour = factor(Month))) +
  geom_miss_point()

Created on 2018-04-17 by the reprex package (v0.2.0).

Let me know how you go, @jminnier ? And thank you so much for filing an issue!

@jminnier
Copy link
Author

Ah, awesome, thanks so much @seasmith! It works perfectly, now. Keep up the great work, both of you!

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

Successfully merging a pull request may close this issue.

3 participants