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

Comments

Projects
3 participants
@jminnier

jminnier commented Feb 19, 2018

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

This comment has been minimized.

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 njtierney added the V0.3.0 label Feb 28, 2018

@njtierney

This comment has been minimized.

Owner

njtierney commented Feb 28, 2018

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

This comment has been minimized.

Owner

njtierney commented Feb 28, 2018

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

@njtierney njtierney added this to visualisation in CRAN Version 0.3.0 Feb 28, 2018

seasmith added a commit to seasmith/naniar that referenced this issue Apr 13, 2018

njtierney added a commit that referenced this issue Apr 17, 2018

Merge pull request #144 from seasmith/master
Correct 'fill' aesthetic behavior in geom_miss_point() #137
@njtierney

This comment has been minimized.

Owner

njtierney commented Apr 17, 2018

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

This comment has been minimized.

jminnier commented Apr 19, 2018

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

@njtierney njtierney moved this from visualisation to Done in CRAN Version 0.3.0 Apr 19, 2018

@njtierney njtierney removed the V0.3.0 label Jun 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment