2

I'm attempting to set manual colors for Dimple dPlot line values and having some trouble.

d1 <- dPlot(
   x="Date", 
   y="Count",
   groups = "Category",
   data = AB_DateCategory,
   type = 'line'
  )

d1$xAxis(orderRule = "Date")
d1$yAxis(type = "addMeasureAxis")
d1$xAxis(
     type = "addTimeAxis",
     inputFormat = "%Y-%m-%d",
     outputFormat = "%Y-%m-%d",
     )

The plot comes out looking great, but I would like to manually set the "Category" colors. Right now, it's set to the defaults and I cannot seem to find a method of manually setting a scale.

I have been able to set the defaults using brewer.pal, but I want to match other colors in my report:

d1$defaultColors(brewer.pal(n=4,"Accent"))

Ideally, these are my four colors - the category values I'm grouping on are R, D, O and U.

("#377EB8", "#4DAF4A", "#E41A1C", "#984EA3"))
Mark S
  • 471
  • 3
  • 9

2 Answers2

2

If I understand correctly, you want to make sure R is #377EB8, etc. To match R, D, O, U consistently to the colors especially across multiple charts, you will need to do something like this.

d1$defaultColors = "#!d3.scale.ordinal().range(['#377EB8', '#4DAF4A', '#E41A1C', '#984EA3']).domain(['R','D','O','U'])!#"

This is on my list of things to make easier.

Let me know if this doesn't work.

timelyportfolio
  • 6,479
  • 30
  • 33
  • That did it! Just replaced the = with an open ( and closed the whole statement. `d1$defaultColors("#!d3.scale.ordinal().range(['#377EB8', '#4DAF4A', '#E41A1C', '#984EA3']).domain(['R','D','O','U'])!#")` – Mark S Aug 28 '14 at 16:37
  • Follow up to this - it appears this is changing the default color values, but instead of assigning them to the values provided, it's assigning them in order of count. For example, if the value with the highest count is 'U' (count of 5,000), then 'O' (4,000), then 'R' (1,000), then 'D' (500), U is receiving color #377EB8, O #4DAF4A, R #E41A1C, then finally D #984EA3. I'd like to assign the coded values, regardless of order, if that's possible, which isn't currently happening. Let me know what you think if you don't mind, thanks! – Mark S Sep 09 '14 at 15:33
  • This post seemed to lend some hints, especially related to ordering, just not sure how to remedy. http://stackoverflow.com/questions/17321139/mapping-in-d3-ordinal-scales – Mark S Sep 09 '14 at 15:37
0

The issue with the accepted answer above is that defining an ordinal scale will not guarantee that specific colors are bound to specific categories R, D, O and U. The color mapping will change depending on the input data. To assign each color specifically you can use assignColor like this

d1$setTemplate(afterScript = '<script>
myChart.assignColor("R","#377EB8");
myChart.draw();
</script>')
mlist
  • 285
  • 1
  • 9