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

New error about continuous/discrete x comes up with geom_histogram() in ggplot2 2.0 #1465

Closed
sheriferson opened this issue Jan 3, 2016 · 17 comments

Comments

@sheriferson
Copy link

Here is minimal reproducible code to demonstrate my problem:

library(ggplot2)
days <- c("Fri", "Fri", "Fri", "Sat", "Sat", "Sun")
days <- as.data.frame(days)

ggplot(days, aes(x = days)) + geom_histogram(aes(y = ..count..))

This code used to run fine, but after updating to ggplot2 it gives the following error:

Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?

I thought "this is new", but I tried adding stat = "count" anyway:

ggplot(days, aes(x = days)) + geom_histogram(aes(y = ..count..), stat = "count")

Which gives me this error:

Error: Unknown parameters: binwidth, bins, origin, right

Okay, I tried to specify them myself:

ggplot(days, aes(x = days)) + geom_histogram(aes(y = ..count..), stat = "count",
                                             binwidth = 1,
                                             bins = 3,
                                             origin = 1,
                                             right = TRUE)

But it gives me the same error:

Error: Unknown parameters: binwidth, bins, origin, right

I've confirmed that the days variable is a factor (and my old code used to work with that before):

> str(days$days)
 Factor w/ 3 levels "Fri","Sat","Sun": 1 1 1 2 2 3

Any tips on what I'm doing wrong or what's going wrong? I tried looking at earlier issues and at ?geom_histogram.

@has2k1
Copy link
Contributor

has2k1 commented Jan 3, 2016

Use geom_bar

@sheriferson
Copy link
Author

@has2k1 That worked. I'm still curious why geom_historgram stopped working, but thank you.

@has2k1
Copy link
Contributor

has2k1 commented Jan 3, 2016

Previously geom_bar and geom_histogram were the same, one an alias of the other. Now geom_histogram is for continuous data (it will do binning) and geom_bar is for discrete data.

@sheriferson
Copy link
Author

Thanks for the explanation.

@danilecas
Copy link

Same problem here and solved it based on this explanation. Thanks.

@vspinu
Copy link
Member

vspinu commented Jun 14, 2016

geom_bar is not equivalent to geom_histogram. You cannot plot ..density.. anymore. geom_bar(aes(y = ..density..)) gives Error in eval(expr, envir, enclos) (from quickdf.r#11) : object 'density' not found.

What's the canonical way to plot proportions right now?

@vspinu
Copy link
Member

vspinu commented Jun 14, 2016

What's the canonical way to plot proportions right now?

Ok, figured it out. geom_bar(aes(y = ..count../sum(..count..))) does the job, but an extra adjustment of y labels is now needed.

@johnmutiso
Copy link

geom_bar is really working.... great solution
thank you

@paulmajora
Copy link

Thanks using geam_bar() worked fine

@nreedus
Copy link

nreedus commented Sep 12, 2017

This worked for me as well. Thanks

@Arghya999
Copy link

geom_bar() helped me as well.
Thanks.

@kgeier
Copy link

kgeier commented Jan 19, 2018

Why does it think my data is discrete and not continuous?

@batpigandme
Copy link
Contributor

@kgeier it's impossible to tell without more information.

Could you please include self-contained reprex (short for minimal reproducible example) in a question at https://community.rstudio.com/, or (if you're confident it's a bug in ggplot2) a new issue?

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

@kgeier
Copy link

kgeier commented Jan 22, 2018

@batpigandme
Thank you for your reply. Sorry I didn't mean for someone to solve my problem without any information. I was hoping instead for a brief discussion of why geomhistogram occasionally views data that appears continuous as discrete. For instance, I know data can be coded as a factor or character. Are there other issues that can lead to this kind of error?

@iamrishi-x
Copy link

by using geom_bar it give me this error
stat_bin() must not be used with a y aesthetic.

@AnishXavier
Copy link

@risved7 Was the data summarized?

@lock
Copy link

lock bot commented Dec 21, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Dec 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests