2014-09-29 79 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))