Search code examples
rplotlybar-chartstacked

Applying filter causes all levels of a variable to show


I filtered a dataframe and then used 'summarise' to get the metrics I wanted.

df.2 <- df.1 %>% filter(state %in% c("NY", "NJ", "MI", "LA", "WA")) %>% group_by(state) %>% 
  summarise(V1 = max(v1), V2 = max(v2), V3 = max(v3))

Then when I do this...

plot_ly(df.2, x = ~state, y = ~V1, type = "bar", name = "Name1") %>%
  add_trace(y = ~V2, name = "Name2") %>% add_trace(y = ~V3, name = "Name3") %>%
  layout(yaxis = list(title = "Count", barmode = "stack"))

All the states appear on the x-axis albeit specifically filtering on on NY, NJ, etc. Another issue is... the plots are not stacked. How to fix these issues?

Dput:

structure(list(state = structure(1:5, .Label = c("LA", "MI", 
"NJ", "NY", "WA"), class = "factor"), V1 = c(582, 845, 1232, 
5489, 372), V2 = c(16284, 18970, 44416, 138863, 8384), 
    V3 = c(58371, 31362, 50558, 201195, 83391)), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

Solution

  • We can use droplevels on 'df.2' to remove the unused levels (assuming that the column 'state' is factor class)

    df.2 <- droplevels(df.2)
    plot_ly(df.2, x = ~state, y = ~V1, type = "bar", name = "Name1") %>%
      add_trace(y = ~V2, name = "Name2") %>% 
      add_trace(y = ~V3, name = "Name3") %>%
      layout(yaxis = list(title = "Count"), barmode = "stack")
    

    enter image description here


    If we want it in ascending order, change the factor levels after doing an arrange

    df.2 %>%
      arrange(V3, V2, V1) %>% 
      mutate(state = factor(state, levels = unique(state))) %>%
      plot_ly(x = ~state, y = ~V1, type = "bar", name = "Name1") %>%
      add_trace(y = ~V2, name = "Name2") %>%
      add_trace(y = ~V3, name = "Name3") %>%
      layout(yaxis = list(title = "Count"), barmode = "stack")