Open smach opened 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>" )
OK thanks.
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.
@smach have you tried yearNew <- sort(year) xAxis( orderRule = "your group " , groupordderRule = yearNew)?
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" )
That did the trick, thanks!
With this test data frame:
If I create a bar chart with this code
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.