timelyportfolio / rcdimple

htmlwidgets for rCharts + dimple
http://www.buildingwidgets.com/blog/2015/3/18/week-11-dimple-as-htmlwidget
MIT License
28 stars 9 forks source link

Ordering on bar chart changes from RStudio viewer to HTML #7

Open smach opened 9 years ago

smach commented 9 years ago

With this test data frame:

testdata <- data.frame(Precinct = structure(1:18, .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"),
class = c("ordered", "factor")), value = c(2, 2, 3, 1, 1, 1, 0, 3, 1, 4, 2, 0, 5, 6, 4, 7, 8, 7))

If I create a bar chart with this code

dimple(data = testdata, x ="Precinct", y = "value", type = "bar") %>%
default_colors(list("#8B0000")) %>%
add_title( html = "<h2>My test data</h2>" )

the bar chart in RStudio's viewer is ordered by Precinct. However, the same graph embedded in an HTML RMarkdown document or viewed in my default browser with RStudio's "show in new window" option is ordered by descending value. The behavior probably ought to be consistent.

timelyportfolio commented 9 years ago

rcdimple does not handle factors intelligently yet. You can force order with something like this. I intentionally rev the levels to demonstrate the effect. I plan to make this easier in a future iteration.

library(rcdimple)
library(magrittr)

testdata <- data.frame(Precinct = structure(1:18, .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"),
                                            class = c("ordered", "factor")), value = c(2, 2, 3, 1, 1, 1, 0, 3, 1, 4, 2, 0, 5, 6, 4, 7, 8, 7))

dimple(data = testdata, x ="Precinct", y = "value", type = "bar") %>%
  xAxis( orderRule = rev(levels(testdata$Precinct)) ) %>%
  default_colors(list("#8B0000")) %>%
  add_title( html = "<h2>My test data</h2>" )

Alternately, if you wanted to sort by the value, you can do this.

library(rcdimple)
library(magrittr)

testdata <- data.frame(Precinct = structure(1:18, .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"),
                                            class = c("ordered", "factor")), value = c(2, 2, 3, 1, 1, 1, 0, 3, 1, 4, 2, 0, 5, 6, 4, 7, 8, 7))

dimple(data = testdata, x ="Precinct", y = "value", type = "bar") %>%
  xAxis( orderRule = "value"  ) %>%
  default_colors(list("#8B0000")) %>%
  add_title( html = "<h2>My test data</h2>" )
smach commented 9 years ago

OK thanks.

smach commented 9 years ago

Hi just FYI I tried to do a grouped barchart with the group column by year and the years appeared in a random order - nothing I could figure out to make the years appear in order with a chart such as

mydataframe %>% dimple( x = c("month","year"), y = "value", groups = "year", type = "bar" )

so I ended up having to use another library.

happyshows commented 9 years ago

@smach have you tried yearNew <- sort(year) xAxis( orderRule = "your group " , groupordderRule = yearNew)?

timelyportfolio commented 9 years ago

Thanks @happyshows. I was preparing this as I saw your answer. Maybe, something like this?

library(rcdimple)
library(pipeR)

data.frame(
    "year" = rep(2000:2005,each = 12)
    ,"month" = rep(1:12,12)
    ,"value" = runif( 12 * 12, 80, 100 )
) %>>%
    dimple(
        x = c("month","year")
        ,y = "value"
        ,groups = "year"
        ,type = "bar"
    ) %>>%
    xAxis( grouporderRule = "year" )
smach commented 9 years ago

That did the trick, thanks!