Closed cb12991 closed 1 week ago
I was just about to post the same issue. Adding reprex here in case it helps:
library(tidyverse)
library(scales)
# Error
tibble(x = seq(1, 10, 1),
y = seq(10, 9010, 1000)) |>
ggplot(aes(x, y)) +
geom_line() +
scale_y_continuous(labels = label_currency(scale_cut = cut_short_scale()))
#> Error in break_suffix[bad_break][improved_break & !power10_break] <- names(lower_break[improved_break & : NAs are not allowed in subscripted assignments
# Work Okay
tibble(x = seq(0, 10, 1),
y = 10 ^ (0:10)) |>
ggplot(aes(x, y)) +
geom_line() +
scale_y_log10(labels = label_currency(scale_cut = cut_short_scale()))
tibble(x = seq(0, 10, 1),
y = 10 ^ (0:10)) |>
ggplot(aes(x, y)) +
geom_line() +
scale_y_continuous(labels = label_currency(scale_cut = cut_short_scale()))
tibble(x = seq(1, 10, 1),
y = seq(10, 9010, 1000)) |>
ggplot(aes(x, y)) +
geom_line() +
scale_y_log10(labels = label_currency(scale_cut = cut_short_scale()))
Created on 2023-12-03 with reprex v2.0.2
Same issue here, CONFIRMED IT IS SOLVED BY ROLLING BACK TO Scales v1.2.1
Error in break_suffix[bad_break][improved_break & !power10_break] <- names(lower_break[improved_break & : NAs are not allowed in subscripted assignments
Yeah, Same issue with cut_short_scale
. But cut_si
works fine.
here is an example :
Same error as mentioned above
set.seed(123)
expand_grid(date = seq(from = as.Date("2023-12-01")
, to = as.Date("2023-12-06")
, by = "day")
, entity = c("A","B","C","D")) %>%
mutate(cnts = runif(nrow(.),min = 10, max = 1e4)) %>%
ggplot(aes(date, y= cnts, fill = entity, color = entity)) +
geom_point() +
geom_line() +
facet_wrap(entity ~ ., scales = "free") +
scale_y_continuous(labels = scales::label_number(scale_cut = scales::cut_short_scale()))
With cut_si
there is no error!
set.seed(123)
expand_grid(date = seq(from = as.Date("2023-12-01")
, to = as.Date("2023-12-06")
, by = "day")
, entity = c("A","B","C","D")) %>%
mutate(cnts = runif(nrow(.),min = 10, max = 1e4)) %>%
ggplot(aes(date, y= cnts, fill = entity, color = entity)) +
geom_point() +
geom_line() +
facet_wrap(entity ~ ., scales = "free") +
scale_y_continuous(labels = scales::label_number(scale_cut = scales::cut_si("")))
Same issue with other data, and I confirm that rolling back to 1.2.1 solves the problem
How to roll back to Scales v1.2.1? why they do not keep an archive of old versions ?
@msgoussi I used this post as guidance
https://www.r-bloggers.com/2021/01/installing-older-version-of-a-package/
Clearly looks like unintended behavior to me.
scales::label_number(scale_cut = scales::cut_short_scale())(c(0,1000,2000))
#> [1] "0" "1K" "2K"
scales::label_number(scale_cut = scales::cut_short_scale())(c(0,500,1500,2000,2500))
#> Error in break_suffix[bad_break][improved_break & !power10_break] <- names(lower_break[improved_break & : NAs are not allowed in subscripted assignments
scales::label_number(scale_cut = scales::cut_short_scale())(c(0,1000,2000))
#> [1] "0" "1K" "2K"
scales::label_number(scale_cut = scales::cut_short_scale())(c(0,500,1500,2000,2500))
#> [1] "0" "500" "1.5K" "2.0K" "2.5K"
Created on 2023-12-17 with reprex v2.0.2
It seems like the problem comes from this line of code in the function scale_cut
lower_break <- breaks[match(break_suffix[bad_break], names(breaks)) - 1]
In my example above lower_breaks
become 0
which causes trouble with division by zero in the following lines.
It might be sufficient to add one more line and make it:
lower_break <- breaks[match(break_suffix[bad_break], names(breaks)) - 1]
lower_break[lower_break == 0] <- 1
Have encountered the same problem while using ggplot with scale_y_continuous. Have found a workaround that fixed it for me.
library(tidyverse)
# Error in break_suffix[bad_break][improved_break & !power10_break] <- names(lower_break[improved_break & :
# NAs are not allowed in subscripted assignments
tibble(
x = 1:3,
y = as.integer(c(12312, 11931, 11185))
) |>
ggplot() +
geom_col(
aes(x = x, y = y),
) +
scale_y_continuous(
label = scales::label_number(scale_cut = scales::cut_short_scale()),
)
# Workaround: Adding a 1 between 0 and 1000 in the result of scales_cut_short() fixes the error.
library(tidyverse)
tibble(
x = 1:3,
y = as.integer(c(12312, 11931, 11185))
) |>
ggplot() +
geom_col(
aes(x = x, y = y),
) +
scale_y_continuous(
label = scales::label_number(scale_cut = append(scales::cut_short_scale(), 1, 1)),
)
Hi.. I am using many packages and i get the error because i am using scales 1.2.1
namespace ‘scales’ 1.2.1 is already loaded, but >= 1.3.0 is required Failed with error: ‘package ‘ggplot2’ could not be loaded’
please advise.
I have used devtools::install_github("r-lib/scales", ref = "c0f79d3") and it is working fine
Also running into this with a basic reprex:
scales::label_number(big.mark = ",", scale_cut = scales::cut_long_scale(TRUE))(range(palmerpenguins::penguins$body_mass_g, na.rm = TRUE))
#> Error in break_suffix[bad_break][improved_break & !power10_break] <- names(lower_break[improved_break & : NAs are not allowed in subscripted assignments
scales::label_number(big.mark = ",")(range(palmerpenguins::penguins$body_mass_g, na.rm = TRUE))
#> [1] "2,700" "6,300"
Created on 2024-08-22 with reprex v2.1.0
Error occurs when supplying
scale_cut
argument&&
number input actually cut (i.e.,1,000 -> 1K
).For example, no error thrown when number input does not need cutting:
Error thrown when number input requires cutting:
Created on 2023-11-29 with reprex v2.0.2