2016-12-15 29 views
1

使用從ggplot2擴展的ggplotly調用,我試圖給點添加輪廓。如何使用顏色填充來解決ggplotly錯誤

ggplot的呼叫有效,但將呼叫延伸至ggplotly會產生錯誤。

目標是基於具有黑色輪廓的連續變量(在這種情況下使用pch = 21)的顏色填充點的交互式繪圖。

最小的可重複的例子:

library(ggplot2) 
library(plotly) 
ggplot(data=mtcars, aes(mpg, wt, fill=hp))+geom_point(pch=I(21))+scale_fill_continuous(low='red', high='blue') 

enter image description here

ggplot(data=mtcars, aes(mpg, wt, fill=hp))+geom_point(pch=I(21))+scale_fill_continuous(low='red', high='blue')+ggplotly() 

Error: Don't know how to add o to a plot In addition: Warning message: In L$marker$color[idx] <- aes2plotly(data, params, "fill")[idx] : number of items to replace is not a multiple of replacement length

回答

1

就快,但不是對結束對+ggplotly()標記呼叫,則需要圍繞ggplotggplotly

## save the ggplot object 
g <- ggplot(data=mtcars, aes(mpg, wt, fill=hp))+ 
    geom_point(pch=I(21))+ 
    scale_fill_continuous(low='red', high='blue') 

## call ggplotly on the ggplot object 
ggplotly(g) 

雖然,我注意到,在調用ggplotlyfill的不尊重......

直接在plot_ly做到這一點,你可以指定調色板和情節它

pal <- colorRampPalette(c("red","blue"))(length(unique(mtcars$hp))) 

plot_ly(data = mtcars, x = ~mpg, y = ~wt, color = ~hp, colors = pal, 
      type = "scatter", mode = "markers", 
      marker = list(size = 10, 
          line = list(color = "black", 
          width = 2))) 

更多的例子

0參見 here和個

enter image description here

+0

謝謝,因爲你已經注意到輸出不正確顯示填充顏色(這是所有紅色)和仍然存在的錯誤消息... – Minnow

+0

沒錯這就是奇 - 不能完全做出來... – SymbolixAU

+0

我要麼!是否有可能使用基地圖來添加基於因素的填充? – Minnow