ggupset icon indicating copy to clipboard operation
ggupset copied to clipboard

Specifiying stat = 'identity' breaks order_by

Open liamhaller opened this issue 1 year ago • 0 comments

Hi, first off thank you for your work on the package. I am experimenting by trying to add survey weights to the graph and noticed that once stat = 'identity' is specified scale_x_upset is no longer able to order by freq and inconsistently orders by 'degree'.

I would expect the two graphs below to be identical given the only difference is that the values for geom_bar are summarised in advance and the value for the weights are 1 however, the order of the bars remains different.

Thank you in advance!

df <- data.frame(matrix(data = seq(1:500), nrow = 500))
   df$a <- sample( c(0, 1), 500, replace=TRUE, prob=c(0.70, .30))
   df$b <- sample( c(0, 1), 500, replace=TRUE, prob=c(0.40, .60))
   df$c <- sample( c(0, 1), 500, replace=TRUE, prob=c(0.90, .10))
   df$d <- sample( c(0, 1), 500, replace=TRUE, prob=c(0.05, .95))
   df$e <- sample( c(0, 1), 500, replace=TRUE, prob=c(0.50, .50))
   df$weights <- 1
   df <- df %>% select(-1)


   df %>%   
     as_tibble(rownames = "respondent") %>% 
     pivot_longer(-respondent , names_to = 'question') %>% 
     filter(value != 0) %>% 
     group_by(respondent) %>% 
     summarize(question = list(question)) %>% 
     ggplot(aes(x = question)) +
     geom_bar() +
     scale_x_upset(order_by = 'freq')
   
  df %>% 
     as_tibble(rownames = "respondent") %>% 
     pivot_longer(-c(respondent, weights) , names_to = 'question') %>% 
     filter(value != 0) %>% 
     group_by(respondent, weights) %>% 
     summarize(question = list(question)) %>% 
     group_by(question) %>% 
     summarise(count = sum(weights)) %>% 
     ggplot(aes(x = question, y = count)) +
     geom_bar(stat = 'identity') +
     scale_x_upset(order_by = 'freq')

liamhaller avatar Nov 03 '23 22:11 liamhaller