Skip to content

empty plot with secondary axis fails #4509

Closed
@FBartos

Description

@FBartos

Hi,

I randomly encountered a bug while working with the ggplot package. I was trying to create an empty plot with secondary (so other functions in my package can fill it up later). However, adding the secondary axis to an empty ggplot object throws an error if the object is called.

E.g.,:

plot <- ggplot2::ggplot() 
plot <- plot + ggplot2::scale_y_continuous(
  name = "ylab", limits = c(0, 1), 
  sec.axis = ggplot2::sec_axis(~ ., name = "ylab2", breaks = c(0, 1), labels = c(0, 2)))

which throws this error when calling plot

Error in seq.default(range[1], range[2], length.out = self$detail) :
'from' must be of length 1

However, adding a geom (either before or after adding the secondary axis) fixes the issue:

plot + ggplot2::geom_point(data = data.frame(x = 0.5, y = 0.5), mapping = ggplot2::aes(x = x, y = y))

Best,
Frantisek

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorscales 🐍

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions