hrbrmstr / taucharts

:bar_chart: An R htmlwidget interface to the TauCharts javascript library
http://rpubs.com/hrbrmstr/taucharts
Other
65 stars 10 forks source link

Stacked bars? #16

Closed ignacio82 closed 9 years ago

ignacio82 commented 9 years ago

Is is possible to have stacked bars? For example:

#Fake data
effectiveness <-
  structure(
    list(
      Ranking = structure(
        c(
          5L, 4L, 3L, 2L, 1L, 5L,
          4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 1L, 2L, 3L, 4L, 5L
        ), .Label = c("Bottom",
                      "Bellow Average", "Average", "Above Average", "Top"), class = c("ordered",
                                                                                      "factor")
      ), Probability = c(
        0.4, 0.4, 0.1, 0.08, 0.02, 0.1, 0.2,
        0.5, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.01, 0.04, 0.15, 0.7,
        0.1
      ), data = structure(
        c(
          4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L,
          2L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L
        ), .Label = c("none",
                      "little", "some", "lots"), class = c("ordered", "factor")
      )
    ), .Names = c("Ranking",
                  "Probability", "data"), row.names = c(NA,-20L), class = "data.frame"
  )

effectiveness %>% 
  tauchart(.) %>%
  tau_bar(x = "data", y = "Probability", color = "Ranking") %>%
  tau_guide_y(tick_format = "%") %>%
  tau_tooltip() %>% 
  tau_legend()

I would like to have 4 bars, each one adds to 100%

Thanks!

hrbrmstr commented 9 years ago

it turns out the TauCharts API does have support for this. Gimme just a little bit and I shld have it in dev

hrbrmstr commented 9 years ago

if you devtools::install_github("hrbrmstr/taucharts@dev") you should be able to do:

tauchart(effectiveness) %>%
  tau_stacked_bar(x = "data", y = "Probability", color = "Ranking") %>%
  tau_guide_y(tick_format = "%") %>%
  tau_guide_gridlines(FALSE, FALSE) %>% 
  tau_tooltip() %>% 
  tau_legend()

image

hrbrmstr commented 9 years ago

I'd probably add auto_scale = FALSE to tau_guide_y so the max value is 100%, too.

ignacio82 commented 9 years ago

@hrbrmstr looks great. Is it possible to invert the order of the ranking? That is, bottom should be on the bottom and top on the top

ignacio82 commented 9 years ago

This works

effectiveness  %>% arrange(data, Ranking)  %>%tauchart() %>%
  tau_stacked_bar(x = "data", y = "Probability", color = "Ranking") %>%
  tau_guide_y(tick_format = "%", auto_scale = FALSE) %>%
  tau_guide_gridlines(FALSE, FALSE) %>% 
  tau_tooltip() %>% 
  tau_legend()

Thanks!