2014-09-29 47 views
2

我使用R的ggplot创建一个静态图并将其传递给plot.ly以创建一个交互式图。我的目标是将一个分类变量投影到颜色和一个数字变量的大小。有R的[光圈]数据完美的作品 - 就像是:使用R的2+图例plotly/plot.ly

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/

现在,我有我自己的数据集,一,...

> 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                     

当我只有用国家作为分类变量,它也有效...

testplot <- ggplot(a, aes(OR_Edu, OR_Illn, color=Country)) + geom_point() 
py$ggplotly(testplot) 

但是当我试图将'总'映射到th e marker

testplot <- ggplot(a, aes(OR_Edu, OR_Illn, color=Country, size=total)) + geom_point() 
py$ggplotly(testplot) 

尽管总数显然是一个数值,但我得到这个错误。

以L $标记$尺寸误差* marker.size.mult: 非数值参数二元运算

问题是什么?有任何想法吗?

和onther的东西(也许我需要问一个单独的问题):我如何定制悬停时出现的小弹出框?

非常感谢!

回答

2

我通过电子邮件回复了你,但我会在这里为大家发帖。 ;)

您遇到了一个错误,我们能够快速修复它,感谢您的反馈。 :)

请重新安装和重新加载“plotly”包,重新实例化py对象,它现在应该工作:https://plot.ly/~marianne2/38/or-illn-vs-or-edu/

让我在一个单独的职位上你的“悬停”的问题答复。

再次感谢您的反馈!

+0

是的,它现在可以工作 - 非常棒!很高兴玩beta测试者,并感谢制作plot.ly!我现在将发布另一个问题regaridng悬停现在... – Sylvia 2014-10-01 19:35:32

0

好的,我可以将它回复到它所属的位置。要编辑悬停框中显示的内容:

# Load "plotly" 
library(plotly) 
# Open a Plotly connection 
py <- plotly() 

# Retrieve a Plotly graph in R 
hover_text <- py$get_figure("PlotBot", 81) 

str(hover_text$data) 
# This list has 4 elements, which all have dimension (attribute) 'text' 

# You can overwrite them one by one, say 
hover_text$data[[1]]$text[1] 
hover_text$data[[1]]$text[1] <- "US" 

# If you need something functional, I would recommend defining the necessary 
# functions and using sapply() 

# Plotly graph with hover text we just edited 
py$plotly(hover_text$data, kwargs=list(layout=hover_text$layout))