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

stat_count does not work with line #2274

Closed
dpmcsuss opened this issue Sep 25, 2017 · 4 comments
Closed

stat_count does not work with line #2274

dpmcsuss opened this issue Sep 25, 2017 · 4 comments
Labels
feature a feature request or enhancement layers 📈

Comments

@dpmcsuss
Copy link

When I make an empirical probability mass functions (pmf), I like to use linerange with ymin=0 and ymax equal to the value of the empirical pmf. stat_count works well but I cannot use linerange with it without using kind of weird hack.

library(ggplot2)
# works and gives a bar chart
ggplot(mtcars,aes(x=gear))+stat_count()

# throws an error
ggplot(mtcars,aes(x=gear))+stat_count(aes(ymax=..prop..,ymin=0),geom="linerange")
# Error: stat_count() must not be used with a y aesthetic.

# this works because it does not populate the ymin variable
ggplot(mtcars,aes(x=gear))+stat_count(aes(ymax=..prop..,ymin=..prop..-..prop..),geom="linerange")

The reason that it fails is because when I set ymin=0 in aes, the data variable has a column with name ymin and is.null(data$y) returns true which results in the error. The hack involves setting ymin=..prop..-..prop.. in the aes call.

if (!is.null(data$y)) {

@has2k1
Copy link
Contributor

has2k1 commented Sep 25, 2017

How about?

ggplot(mtcars,aes(x=gear))+stat_count(width=0.005)

For one, the desirable width will depend on the x scale. It is less wierd though.

@dpmcsuss
Copy link
Author

Yes there are definitely some workarounds and not sure if this is really a bug but it is somewhat weird (though very R) behavior. To me, the width solution is less appealing than theymin=..prop..-..prop.. solution.

@hadley hadley added feature a feature request or enhancement layers 📈 labels Oct 30, 2017
@hadley
Copy link
Member

hadley commented Oct 31, 2017

I think this will be fixed by #2059, since it would allow you to write:

ggplot(mtcars, aes(gear)) + 
  stat_count(aes(ymax = computed(prop), ymin = 0, y = computed(0)), geom="linerange")

Otherwise, given that there are other ways to solve the problem (i.e. counting yourself) and it affects few people, I unfortunately don't have the time to make a fix.

@hadley hadley closed this as completed Oct 31, 2017
@dpmcsuss
Copy link
Author

Thanks. That looks like it will be a nice solution and I definitely understand that it is a minor issue.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement layers 📈
Projects
None yet
Development

No branches or pull requests

3 participants