r - 如何设置 googleVis 气泡图的 colorAxis?
问题描述
我尝试使用基于利润列的气泡连续色标来调整 GoogleVis 气泡图示例:
library(googleVis)
Bubble <- gvisBubbleChart(Fruits, idvar="Fruit",
xvar="Sales", yvar="Expenses",
colorvar="Profit",
options=list(
## custom color
## colors = "['red', 'green', 'blue']",
## custom color scale does not work
colorAxis="{colors: ['yellow', 'red']}",
hAxis='{minValue:75, maxValue:125}'))
plot(Bubble)
但是,无论我尝试使用“colorAxis”选项,它都不起作用,而我确实遵循了官方文档。看起来很奇怪,因为当我查看情节的源代码时,我可以看到该选项已设置:
// jsDrawChart
function drawChartBubbleChartID2b6add84971() {
var data = gvisDataBubbleChartID2b6add84971();
var options = {};
options["colorAxis"] = {colors: ['yellow', 'red']};
options["hAxis"] = {minValue:75, maxValue:125};
var chart = new google.visualization.BubbleChart(
document.getElementById('BubbleChartID2b6add84971')
);
chart.draw(data,options);
}
我究竟做错了什么?谢谢你的帮助。
解决方案
更新:
我在 GitHub 上添加了一个问题后,该问题应该在 GitHub 上的当前开发者版本中得到解决(devtools::install_github("mages/googleVis")
)。确实,它有效:
问题似乎是数据列“利润”:
var datajson = [
["Apples", 98, 78, "20"],
["Oranges", 96, 81, "15"],
["Bananas", 85, 76, "9"]
];
data.addColumn('string','Fruit');
data.addColumn('number','Sales');
data.addColumn('number','Expenses');
data.addColumn('string','Profit');
由于我没有看到此列被定义为string
类型列的原因。当您将相关行更改为
var datajson = [
["Apples", 98, 78, 20],
["Oranges", 96, 81, 15],
["Bananas", 85, 76, 9]
];
data.addColumn('number','Profit');
渐变色标应用于气泡。
这可能是googleVis
实现中的一个错误,因为Fruits
数据将利润列定义为数字。