Skip to content
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

v0.5.0 does not add new fill scale in some cases #70

Open
owenssam1 opened this issue Aug 19, 2024 · 3 comments
Open

v0.5.0 does not add new fill scale in some cases #70

owenssam1 opened this issue Aug 19, 2024 · 3 comments

Comments

@owenssam1
Copy link

owenssam1 commented Aug 19, 2024

I had an issue with the newest version adding a fill scale, but when I reverted to 0.4.10, the issue went away. I was attempting to add a new fill scale via the code below, which had worked before I updated the package and works now that I have reverted.

Here is the error I was receiving:

"Error in $<-.data.frame(*tmp*, ".label", value = c("low", "moderate", : replacement has 4 rows, data has 3".

I was able to isolate the issue to the geom_point, which worked if I used fill outside the aes statement as just a single color. But as is, with the geom_point as the 2nd fill scale, the code will work up until the geom_point statement, but will not work if you try to include a second fill scale.

Here is the code I used:

 slf_binarized_2055_plot <- ggplot() +
      map_style +
      # data layer
      geom_raster(data = slf_binarized_2055_df, aes(x = x, y = y, fill = as.factor(global_regional_binarized))) +
      # add province layer
      geom_sf(data = locality_sf_plot_layer, aes(geometry = geometry), fill = NA, color = "black", linewidth = 0.3) +
      # fill scale raster
      scale_discrete_manual(
        name = "projected risk",
        values = values.obj,
        breaks = breaks.obj,
        labels = labels.obj,
        aesthetics = "fill",
        guide = guide_colorsteps(frame.colour = "black", ticks.colour = "black", barwidth = 20, draw.ulim = TRUE, draw.llim = TRUE)
      ) +
      # new scale
      ggnewscale::new_scale_fill() +
      # IVRs
## ISSUE STARTS HERE
      geom_point(data = IVR_locations_plot_layer, aes(x = x, y = y, fill = "viticultural\narea"), size = 2.5, shape = 21) +
      # fill scale for points
      scale_fill_manual(name = "", values = c("viticultural\narea" = "purple3")) +
      # aesthetics
      guides(fill = guide_legend(ncol = 1, byrow = TRUE, override.aes = list(size = 3))) +
      coord_sf()
@eliocamp
Copy link
Owner

Thanks for the report. In your example code your using datasets that I don't have access to, so I cannot reproduce the problem. Could you create a minimal reproducible example that uses as few extraneous objects as possible? You can either use build-in dataset or a minimal dataset that you can share here.

@owenssam1
Copy link
Author

Sure! here are some minimal datasets:

library(terra)
library(rnaturalearthhires)

slf_binarized_1995_df <- data.frame(
  x = c(-156.6251, -156.5418, -156.4585, -156.3751, -156.8751),
  y = c(71.37486, 71.37486, 71.37486, 71.37486, 71.29153),
  global_regional_binarized = rep(5, 5)
)


IVR_locations_plot_layer <- data.frame(
  x = c(-123.61764, -123.53982, -123.46515),
  y = c(40.92875, 38.95189, 39.0750)
)

As for the locality_sf_plot_layer, you can use the package rnaturalearth to access the admin_1_states_provinces dataset: 

locality_sf <- rnaturalearth::ne_load(
      scale = 10,
      type = "admin_1_states_provinces",
      category = "cultural",
      destdir = ,
      returnclass = "sf"
    ) %>%
dplyr::filter(name == `United States`) # I do not actually remember the exact spelling of the USA in this dataset

Hope this helps

@eliocamp
Copy link
Owner

This might be a problem unrelated to ggnewscale. I get the same error just trying to plot the sf object.

library(ggplot2)

locality_sf <- rnaturalearth::ne_download(
  scale = 10,
  type = "admin_1_states_provinces",
  category = "cultural",
  returnclass = "sf"
) |> 
  dplyr::filter(name == "United States") 

ggplot() +
  geom_sf(data = locality_sf)
#> Error in `geom_sf()`:
#> ! Problem while setting up geom aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `$<-.data.frame`:
#> ! replacement has 1 row, data has 0

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

No branches or pull requests

2 participants