legend - R Plotly stacked bar chart issue -
i using plotly create stacked bar chart below
df <- data.frame( var1 = c("ny", "ny", "ny", "sfo", "sfo", "sfo"), var2 = c("low", "medium", "high", "low", "medium", "high"), value = c(10,15,20,10,20,25) ) df$var2 <- factor(df$var2, levels = c("low","medium","high")) # sort factor table df <- arrange(df, var2) col <- list( color = ifelse( df$var2 == "low", "steelblue", ifelse( df$var2 == "medium", "darkorange", "olivedrab" ) )) # plot plot_ly( df, x = ~ var1, y = ~ value, color = ~ var2, marker = col, type = "bar" ) %>% layout(barmode = "stack")
there 2 issues facing: first legend not coming correctly (see image).
second since have dynamic table, there possibility there 1 value in var1 , var2
df <- data.frame(var1 = c("ny"), var2 = c("low"), value = c(10)) df$var2 <- factor(df$var2, levels = c("low", "medium", "high")) # sort factor table df <- arrange(df, var2) col <- list(color = ifelse( df$var2 == "low", "steelblue", ifelse(df$var2 == "medium", "darkorange", "olivedrab") )) # plot plot_ly( df, x = ~ var1, y = ~ value, color = ~ var2, marker = col, type = "bar" ) %>% layout(barmode = "stack")
in case chart doesn't show up. if have 2 values either in var1 or in var2 chart still shows not one. can tell me how handle this?
thanks, manoj
Comments
Post a Comment