在rCharts + polychart中手动设置点颜色 [英] Manually set point colors in rCharts + polychart

查看:114
本文介绍了在rCharts + polychart中手动设置点颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用rCharts和polychart前端构建图表:

I try to build a chart using rCharts and polychart frontend:

dtf <- data.frame(x=c(1, 2, 3, 4, 5), y=c(4, 5, 6, 3, 5), 
              label=c('one', 'two', 'one', 'two', 'two'))
color.mapping <- list(one='#ff2385', two='#229922')
p2 <- rPlot(x='x', y='y', data=dtf, type='point', color='label')
print(p2)

我想通过使用某种离散映射(如上例)或其他某种逻辑来控制点颜色.怎么做

I would like to have a control over the point colors by either using some sort of discrete mapping (like in the example above) or using some other logic. How is that done

编辑

按照 Ramnath的回答,我尝试执行以下操作,但页面空白:

following the Ramnath's answer I tried to do the following, but I get an empty page:

dtf <- data.frame(x=c(1, 2, 3, 4, 5), y=c(4, 5, 6, 3, 5), 
                  label=c('one', 'two', 'one', 'two', 'two'))

p2 <- rPlot(x='x', y='y', data=dtf, type='point', color='label')

p2$guides(color = list(scale = "#! function(value){
   color_mapping = {one: '#ff2385', two: '#229922'}
   return color_mapping[value];                  
 } !#"))


print(p2)

解决方案 从github升级rCharts已经解决了这个问题.

Resolution upgrading rCharts from github has solved the problem.

推荐答案

当前在Polychart中这有点复杂,尽管我们正在尝试创建一个更加R友好的解决方案.但目前,这是在Polychart中设置自定义比例的方法

This is a little complicated in Polychart currently, although we are trying to create a more R friendly solution. But for now, here is the way to set custom scales in Polychart

p2$guides(color = list(scale = "#! function(value){
  color_mapping = {one: '#ff2385', two: '#229922'}
  return color_mapping[value];                  
} !#"))

这个想法是为色彩美学设定一个尺度.但是,您可以指定一个函数,而不是使用调色板,该函数会根据映射返回一种颜色.您需要将函数包装在#!!#之间,以便rCharts将其视为JS文字,并且在转换为JSON时不对其进行字符串化.

The idea is to set a scale for the color aesthetic. But instead of using a color palette, you specify a function, which returns a color based on the mapping. You need to wrap the function between #! and !# so that rCharts treats it as a JS literal, and does not stringify it when converting to JSON.

如果不确定如何在JSON中指定color_mapping,则可以使用函数toJSON转换R对象.唯一需要注意的是避免在函数内部使用双引号,因为双引号容易被转义,从而导致错误.

If you are unsure how to specify color_mapping in JSON, you can use the function toJSON to convert your R object. Only caveat is avoid using double quotes inside the function, as they tend to get escaped and as a result cause errors.

希望这很有用.

这篇关于在rCharts + polychart中手动设置点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆