munched_lines warning in ggplot2

Viewed 64

I am getting a weird error from ggplot2 only when using a very specific combination of plot features. I can't figure out why this is happening!

Consider this data:

point.dat <- tibble(r = c(0.686, 0.835, 1.016, 1.235, 1.503, 1.828, 2.224, 2.706, 3.292, 4.005), theta = 11.2)
area.dat <- tibble(tmin = 5.62, tmax = 16.9, r = c(3, 3.65))

This data is used to make two different geoms:

area.geom <- geom_ribbon(aes(ymin = tmin, ymax = tmax, x = r), data = area.dat)
point.geom <- geom_point(aes(x = r, y = theta), data = point.dat)

When plotting this data like this, a warning is thrown, though the plot still looks as desired:

ggplot() +
  area.geom +
  point.geom + 
  lims(x = c(0, NA), y = c(0, 360)) +
  coord_polar(theta = "y", start = -pi / 2, direction = -1)

Warning message:
In munched_lines$id + rep(c(0, max(ids, na.rm = TRUE)), each = length(ids)) :
  longer object length is not a multiple of shorter object length

enter image description here

It looks like the line of code that the warning refers to is with geom_ribbon(). See it here at line #160. So, I removed this line from the call, and the warning went away:

ggplot() +
  #area.geom +
  point.geom + 
  lims(x = c(0, NA), y = c(0, 360)) +
  coord_polar(theta = "y", start = -pi / 2, direction = -1)

enter image description here

However, the warning also goes away, no matter which line from the plot call I remove. So, it seems like the warning is only thrown as a result of some faulty communication among these various features. Why is this happening? Can I do something to fix it?

ggplot() +
  area.geom +
  #point.geom + 
  lims(x = c(0, NA), y = c(0, 360)) +
  coord_polar(theta = "y", start = -pi / 2, direction = -1)

enter image description here

ggplot() +
  area.geom +
  point.geom + 
  #lims(x = c(0, NA), y = c(0, 360)) +
  coord_polar(theta = "y", start = -pi / 2, direction = -1)

enter image description here

ggplot() +
  area.geom +
  point.geom + 
  lims(x = c(0, NA), y = c(0, 360)) #+
  #coord_polar(theta = "y", start = -pi / 2, direction = -1)

enter image description here

1 Answers

I can reproduce the warning and I think that it might be a bug related to the outline in the geom_area(). Side note: the munching of the lines is to break them up into small segments so they can form smoother curves in polar coordinate space, so that is where the error occurs.

This is the minimal bit of code that I could image that still forms the warning:

library(ggplot2)

ggplot() +
  geom_area(aes(x = c(1, 2), 
                y = c(3, 4))) +
  coord_polar()
#> Warning in munched_lines$id + rep(c(0, NA), each = length(ids)): longer object
#> length is not a multiple of shorter object length

The reason I think it is outline related, is because the warning doesn't occur when outline.type = "full". I think this warning probably shouldn't happen, because you haven't set a colour, so outline calculations seem a bit superfluous in this case.

ggplot() +
  geom_area(aes(x = c(1, 2), 
                y = c(3, 4)),
            outline.type = "full") +
  coord_polar()

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

Going back to your code, I think you can prevent the warning by adjusting it as follows:

area.geom2 <- geom_ribbon(aes(ymin = tmin, ymax = tmax, x = r), 
                          data = area.dat,
                          outline.type = "full")

ggplot() +
  area.geom2 +
  point.geom + 
  lims(x = c(0, NA), y = c(0, 360)) +
  coord_polar(theta = "y", start = -pi / 2, direction = -1)

I think it might be a good idea to post an issue on ggplot2's github page. As far as I'm aware, they are preparing for the release of version 3.3.1, so I don't know if they can fix this before then.

Related