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

Computation failed in stat_function() for non-numeric values of x #1509

Closed
atfg opened this issue Jan 20, 2016 · 1 comment
Closed

Computation failed in stat_function() for non-numeric values of x #1509

atfg opened this issue Jan 20, 2016 · 1 comment

Comments

@atfg
Copy link

atfg commented Jan 20, 2016

Drawing a line with stat_function for x as factor used to work in ggplot2_1.0.0 and ggplot2_1.0.1. Doesn't work in ggplot2_2.0.0:

ggplot(iris,aes(x=factor(Species),Petal.Length)) + geom_violin() + stat_function(fun=function (x) as.numeric(x)^2, colour="red")

This works in ggplot2_2.0.0, but the x labels need fixing:

ggplot(iris,aes(x=as.numeric(Species),Petal.Length,group=as.numeric(Species))) + geom_violin() + stat_function(fun=function (x) as.numeric(x)^2, colour="red")
@klmr
Copy link
Contributor

klmr commented Jan 20, 2016

It’s worth noting that the x labels cannot be fixed by adding a scale_x_discrete — this will result in the same error as when passing a categorical variabel to the function.

However, using a scale_x_continuous works:

ggplot(iris) +
    aes(as.numeric(Species), Petal.Length, group = Species) +
    geom_violin() +
    stat_function(fun = function (x) as.numeric(x) ^ 2, color = "red") +
    scale_x_continuous('Species',
                       breaks = seq_len(nlevels(iris$Species)),
                       labels = levels(iris$Species))

@hadley hadley changed the title breaking change - Computation failed in stat_function() for non-numeric values of x Computation failed in stat_function() for non-numeric values of x Jan 22, 2016
@hadley hadley closed this as completed in 0ce1c74 Jan 23, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 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

2 participants