Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
smach opened this issue Mar 30, 2015 · 6 comments
Open

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

smach opened this issue Mar 30, 2015 · 6 comments

Comments

@smach
Copy link

smach commented Mar 30, 2015

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
Copy link
Owner

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
Copy link
Author

smach commented Mar 31, 2015

OK thanks.

@smach
Copy link
Author

smach commented Jul 8, 2015

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
Copy link

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

@timelyportfolio
Copy link
Owner

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
Copy link
Author

smach commented Jul 10, 2015

That did the trick, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants