Skip to content

stat_function() doesn't work without dummy data #3983

Closed
@clauswilke

Description

@clauswilke

I would have expected the second and third example below to work, but it doesn't.

library(ggplot2)

# works
ggplot(data.frame(x = c(-5, 5)), aes(x)) + 
  stat_function(fun = dnorm)

# doesn't work
ggplot() + xlim(-5, 5) + 
  stat_function(fun = dnorm)

# doesn't work
ggplot() + 
  stat_function(fun = dnorm, xlim = c(-5, 5))

Created on 2020-05-01 by the reprex package (v0.3.0)

The problem seems to be that stat_function() doesn't return any data in the second and third case, or the data gets somehow deleted. It would be nice if we could solve this before the 3.3.1 release, I assume (hope!) it's a simple fix.

library(ggplot2)

p <- ggplot(data.frame(x = c(-5, 5)), aes(x)) + 
  stat_function(fun = dnorm)
head(layer_data(p))
#>      x            y PANEL group colour size linetype alpha
#> 1 -5.0 1.486720e-06     1    -1  black  0.5        1    NA
#> 2 -4.9 2.438961e-06     1    -1  black  0.5        1    NA
#> 3 -4.8 3.961299e-06     1    -1  black  0.5        1    NA
#> 4 -4.7 6.369825e-06     1    -1  black  0.5        1    NA
#> 5 -4.6 1.014085e-05     1    -1  black  0.5        1    NA
#> 6 -4.5 1.598374e-05     1    -1  black  0.5        1    NA

p <- ggplot() + xlim(-5, 5) + 
  stat_function(fun = dnorm)
layer_data(p)
#> data frame with 0 columns and 0 rows

p <- ggplot() + 
  stat_function(fun = dnorm, xlim = c(-5, 5))
layer_data(p)
#> data frame with 0 columns and 0 rows

Created on 2020-05-01 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions