Skip to content

Unexpected warning when plotting geom_map() #3454

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

Closed
edgararuiz-zz opened this issue Jul 24, 2019 · 8 comments
Closed

Unexpected warning when plotting geom_map() #3454

edgararuiz-zz opened this issue Jul 24, 2019 · 8 comments

Comments

@edgararuiz-zz
Copy link

reprex:

library(ggplot2)
wr <- map_data("world")
ggplot() +
  geom_map(aes(long, lat, map_id = region), map = wr, data = wr)
#> Warning: Ignoring unknown aesthetics: x, y
@paleolimbot
Copy link
Member

I think the problem is that GeomMap inherits from GeomPolygon but overrides required_aes, which should probably be c("map_id", "x", "y").

ggplot2/R/geom-map.r

Lines 118 to 122 in d1ecd03

)
},
required_aes = c("map_id")
)

@paleolimbot paleolimbot added bug an unexpected problem or unintended behavior layers 📈 labels Jul 24, 2019
@yutannihilation
Copy link
Member

I don't know well about geom_map(), but isn't the warning right? With goem_map(), you cannot map variables in the usual plot/layer data to x and y; you need to pass a separate data to map argument and aes() does nothing there.

For example, if the data doesn't have a column named x or long, it fails.

library(ggplot2)
wr <- map_data("world")
wr <- dplyr::rename(wr, lon = long)

ggplot() +
  geom_map(aes(lat, lon, map_id = region), map = wr, data = wr2)
#> Error in geom_map(aes(lat, lon, map_id = region), map = wr, data = wr2): all(c("x", "y", "id") %in% names(map)) is not TRUE

Created on 2019-08-19 by the reprex package (v0.3.0)

@yutannihilation
Copy link
Member

Now that we have setup_data(), maybe we can issue a bit friendlier warning when x or y is mapped accidentally?

@clauswilke
Copy link
Member

I don't understand this issue. Reading the documentation of geom_map() (which arguable is rather lacking and unclear), the code posted with this issue is incorrect, and the warning is correct. geom_map() ignores the x and y aesthetics, as the warning says.

The correct way to set the limits is via expand_limits(), like so:

library(ggplot2)
wr <- map_data("world")
ggplot() +
  geom_map(aes(map_id = region), map = wr, data = wr) +
  expand_limits(x = wr$long, y = wr$lat)

Created on 2019-12-19 by the reprex package (v0.3.0)

@yutannihilation
Copy link
Member

Yes, the warning is definitely correct. I meant adding some check like below might help the user find what they should do.

  ignoring_aes <- intersect(names(mapping), c("x", "y", "group"))
  if (length(ignoring_aes) > 0) {
    warn(glue("geom_map() doesn't accept mapping of `x`, `y`, or `group`, ",
              "but expects the data has fixed names of columns `x`, `y` and `id`."))
  }

What do you think? I'm OK to close this issue if there's no chance to improve the warning than simple Ignoring unknown aesthetics.

@clauswilke
Copy link
Member

My concern about the warning is that the warning needs to be evaluated on the specific combination of geom and stat. Are there cases where somebody uses geom_map() with a different stat that can process x and y?

It seems to me that it would be more important to improve the documentation of geom_map() than to add this warning, because the current documentation barely explains anything. And also, we should discourage use of geom_map() in favor of geom_sf(), and we may want to add a sentence to this effect to the documentation.

@yutannihilation
Copy link
Member

Sorry, I forgot to reply here. Thanks, agreed. I didn't think of the case...

And also, we should discourage use of geom_map() in favor of geom_sf(), and we may want to add a sentence to this effect to the documentation.

Sounds good. I've been wondered the status of geom_map().

@yutannihilation yutannihilation added documentation and removed bug an unexpected problem or unintended behavior layers 📈 labels Jan 10, 2020
@thomasp85
Copy link
Member

I'm closing this in favour of #3721

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants