使用R的2+图例绘制/ plot.ly [英] Using 2+ legends from R to plotly / plot.ly
问题描述
我使用R的ggplot创建一个静态图并将其传递给plot.ly以创建一个交互式图。我的目标是将一个分类变量投影到颜色和一个数字变量的大小。使用R的[iris]数据可以完美实现 - 像这样:
testplot< - ggplot(虹膜,aes(Sepal.Length ,Sepal.Width,color = Species,size = Petal.Length))+ geom_point()
py $ ggplotly(testplot)
https://plot.ly/~SyTpp / 11 / sepalwidth-vs-sepallength /
现在,我有自己的数据集,a,...
> a
国家OR_Edu OR_Illn总数
秘鲁1.75 1.67 25367 10
菲律宾1.33 0.43 33543 1
卢旺达0.29 4.00 6443 2
塞内加尔5.00 1.60 32743 3
斯里兰卡12.00 6.33 21743 4
苏丹17.00 0.86 27227 5
坦桑尼亚0.57 0.71 24312 6
乌干达13.00 0.60 35466 7
越南1.62 1.50 34639 8
赞比亚0.86 1.00 16735 9
津巴布韦1.25 1.00 33349 10
>摘要(a)
国家OR_Edu OR_Illn总数
秘鲁:1分钟。 :0.290分钟。 :0.430分钟。 :6443最小。 :1.000
菲律宾:1 1曲线:1.055第1曲线:0.785第1曲线:23028第1曲线:3.500
卢旺达:1中位数:1.620中位数:1.000中位数:27227中位数:6.000
塞内加尔:1平均值:4.970平均值:1.791平均值:26506平均值:5.909
斯里兰卡:1第3次:8.500第3次:1.635第3次:33446第3次:8.500
苏丹:1最大。 :17.000最大。 :6.330最大。 :35466最大。 :10.000
(其他):5
当我仅使用国家作为分类变量时,它也可以工作......
testplot< - ggplot(a,aes(OR_Edu,OR_Illn,color = Country)) + geom_point()
py $ ggplotly(testplot)
但是当我尝试映射'总计'到标记的大小
testplot< - ggplot(a,aes(OR_Edu,OR_Illn,color = Country, size = total))+ geom_point()
py $ ggplotly(testplot)
这个错误,尽管'总'显然是一个数值。
L $ marker $ size * marker.size.mult错误:
二元运算符的非数字参数
有什么问题?任何想法?
另外一件事(也许我需要在另一个问题中提出这个问题):我如何定制悬停时出现的小弹出框?
非常感谢!
我通过电子邮件回复您,我会在这里为大家发布。 ;)
您遇到了一个错误,我们可以快速修复它,这要归功于您的反馈。 :)
请重新安装并重新加载plotly包,重新实例化您的 py
对象,它应该可以正常工作现在: https://plot.ly/~marianne2/38/ or-illn-vs-or-edu /
让我在单独的帖子中回答你的'悬停'问题。 b
再次感谢您的反馈!
I am using R's ggplot to create a static plot and pass that on to plot.ly to create an interactive plot. My aim to project a categorical variable to the color and a numeric variable to size. With R's [iris] data that works perfect - Like that:
testplot <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species, size=Petal.Length)) + geom_point()
py$ggplotly(testplot)
https://plot.ly/~SyTpp/11/sepalwidth-vs-sepallength/
Now, I have my own dataset, a, ...
> a
Country OR_Edu OR_Illn total num
Peru 1.75 1.67 25367 10
Philippines 1.33 0.43 33543 1
Rwanda 0.29 4.00 6443 2
Senegal 5.00 1.60 32743 3
Sri Lanka 12.00 6.33 21743 4
Sudan 17.00 0.86 27227 5
Tanzania 0.57 0.71 24312 6
Uganda 13.00 0.60 35466 7
Vietnam 1.62 1.50 34639 8
Zambia 0.86 1.00 16735 9
Zimbabwe 1.25 1.00 33349 10
> summary(a)
Country OR_Edu OR_Illn total num
Peru :1 Min. : 0.290 Min. :0.430 Min. : 6443 Min. : 1.000
Philippines:1 1st Qu.: 1.055 1st Qu.:0.785 1st Qu.:23028 1st Qu.: 3.500
Rwanda :1 Median : 1.620 Median :1.000 Median :27227 Median : 6.000
Senegal :1 Mean : 4.970 Mean :1.791 Mean :26506 Mean : 5.909
Sri Lanka :1 3rd Qu.: 8.500 3rd Qu.:1.635 3rd Qu.:33446 3rd Qu.: 8.500
Sudan :1 Max. :17.000 Max. :6.330 Max. :35466 Max. :10.000
(Other) :5
When I use only the country as categorical variable, it works, too...
testplot <- ggplot(a, aes(OR_Edu, OR_Illn, color=Country)) + geom_point()
py$ggplotly(testplot)
but when I try to map 'total' to the size of the marker
testplot <- ggplot(a, aes(OR_Edu, OR_Illn, color=Country, size=total)) + geom_point()
py$ggplotly(testplot)
I get this error, despite 'total' obviously being a numeric value.
Error in L$marker$size * marker.size.mult : non-numeric argument to binary operator
What is the problem? Any ideas?
And onther thing (maybe I need to ask that in a separate question): How can I customise the little pop-up box that appears on hover?
Thanks a lot!
I replied to you over email but I'll post here for everyone. ;)
You ran into a bug, and we were able to fix it quickly thanks to your feedback. :)
Please reinstall and reload the "plotly" package, re-instantiate your py
object and it should work now: https://plot.ly/~marianne2/38/or-illn-vs-or-edu/
Let me reply on your 'hover' question in a separate post.
Thanks again for the feedback!
这篇关于使用R的2+图例绘制/ plot.ly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!