Dimple dPlot color x-axis bar values in R

ぐ巨炮叔叔 提交于 2019-12-08 02:52:12

问题


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"))

回答1:


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.




回答2:


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>')


来源:https://stackoverflow.com/questions/25514731/dimple-dplot-color-x-axis-bar-values-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!