davidsjoberg / ggstream

A package to make streamplots
Other
173 stars 15 forks source link

Code Gives this Error #16

Open benjaminwnelson opened 3 years ago

benjaminwnelson commented 3 years ago

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

davidsjoberg commented 3 years ago

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

benjaminwnelson commented 3 years ago

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

noahtolsen commented 3 years ago

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()
wurli commented 3 years ago

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)
AntonioAlegriaH commented 2 years ago

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()