ggstream icon indicating copy to clipboard operation
ggstream copied to clipboard

Code Gives this Error

Open benjaminwnelson opened this issue 3 years ago • 5 comments

Any idea why it's giving this error?

Error in data.frame(x = full_values$x, y = yy[, iStream * 2], group = as.integer(.group)) : arguments imply differing number of rows: 1000, 1126, 1

benjaminwnelson avatar May 06 '21 19:05 benjaminwnelson

I had someone else have the same error but I don't have a reproducible example. Do you have one?

davidsjoberg avatar May 06 '21 19:05 davidsjoberg

Unfortunately, I don't. I'll look into it more

benjaminwnelson avatar May 07 '21 19:05 benjaminwnelson

I think I've figured out what the issue is here. When the grouping variable is numeric, I tend to get this error. But if I convert it to a character it seems to fix things. Reproducible example of failing and working code below!

Broken because of numeric grouping variable:

library(ggstream)
library(ggplot2)
library(dplyr)

blockbusters %>% 
  mutate(genre2 = if_else(genre == 'Action', 1,
                          if_else(genre == 'Adventure', 2,
                                  if_else(genre == 'Animation', 3,
                                          if_else(genre == 'Comedy', 4,
                                                  if_else(genre == 'Drama', 5, 6)))))) %>% 
ggplot(aes(year, box_office, fill = genre2)) +
  geom_stream()

Working because grouping variable is a character:

library(ggstream)

ggplot(blockbusters, aes(year, box_office, fill = genre)) +
  geom_stream()

noahtolsen avatar Jun 15 '21 17:06 noahtolsen

I'm getting this error and it seems to be because n_grid has to be at least 2% more than the number of observations per group. Here's a reprex:

set.seed(123)

library(ggplot2)
library(ggstream)

test_bug <- function(n_observations, n_grid) {
  
  data.frame(
    x = rep(1:n_observations, 4),
    y = rnorm(4 * n_observations),
    fill = rep(letters[1:4], each = n_observations)
  ) %>% 
    ggplot(aes(x = x, y = y, fill = fill)) + 
    geom_stream(n_grid = n_grid)
  
}

# Fails
test_bug(n_observations = 100, n_grid = 101)
# Works
test_bug(n_observations = 100, n_grid = 102)

# Fails
test_bug(n_observations = 1000, n_grid = 1019)
# Works
test_bug(n_observations = 1000, n_grid = 1020)

wurli avatar Jul 20 '21 22:07 wurli

I'm getting this error and it seems to be because n_grid has to be at least 2% more than the number of observations per group. Here's a reprex:

set.seed(123)

library(ggplot2)
library(ggstream)

test_bug <- function(n_observations, n_grid) {
  
  data.frame(
    x = rep(1:n_observations, 4),
    y = rnorm(4 * n_observations),
    fill = rep(letters[1:4], each = n_observations)
  ) %>% 
    ggplot(aes(x = x, y = y, fill = fill)) + 
    geom_stream(n_grid = n_grid)
  
}

# Fails
test_bug(n_observations = 100, n_grid = 101)
# Works
test_bug(n_observations = 100, n_grid = 102)

# Fails
test_bug(n_observations = 1000, n_grid = 1019)
# Works
test_bug(n_observations = 1000, n_grid = 1020)

This worked for me. But also being especific with the exact number of observations inside n_grid()

AntonioAlegriaH avatar Sep 16 '22 18:09 AntonioAlegriaH