plotly / plotly/plotly.R

`ggplotly` error on plot with `geom_contour` when one level of a factor isn't represented

Open
#2,205 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

Hi R Plotly Devs,
I hope this finds you well!
This issue involves a dataset with a factor where one factor level isn't represented.
When rendering a ggplot that plots a contour, ggplot will provide informative warnings that zero contours were drawn due to the missing factor level.
However, when this plot object is passed into ggplotly the following error is thrown:

Warning: stat_contour(): Zero contours were generated
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Error in `$<-.data.frame`(`*tmp*`, "fill", value = NA) : 
  replacement has 1 row, data has 0

I hope this is a relatively easy fix!

df <- tibble::tribble(
  ~response,        ~variable,      ~prediction,
  7.45549,          6.24703, "No",
  7.45549, 6.62491208333333, "No",
  7.45549, 7.00279416666667, "No",
  7.45549,       7.38067625, "No",
  7.45549, 7.75855833333333, "No",
  7.45549, 8.13644041666667, "No"
) |> 
  dplyr::mutate(prediction = factor(prediction, levels = c("Yes", "No")))
p = ggplot2::ggplot(data = df) +
  ggplot2::geom_point(data = df,
                      ggplot2::aes(
                        x = .data$response,
                        y = .data$variable,
                        color = prediction),
                      shape = 15,
                      size = 0.7,
                      alpha = 0.4) +
  ggplot2::geom_contour(data = df,
                        ggplot2::aes(x = .data$response,
                                     y = .data$variable,
                                     z = as.integer(prediction)),
                        color = "violet",
                        size = 0.5,
                        bins = 1)
plotly::ggplotly(p)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproducible R example in the issue and tracing how ggplotly handles the zero-contour layer produced by the missing factor level. Done means the plot no longer throws the data-frame replacement error while retaining the informative zero-contour warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.