How to resolve ggplotly error using color fill

2019-09-30 11:18发布

使用ggplotly从GGPLOT2延长通话,我想大纲增加了点。

调用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')

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

Answer 1:

你几乎没有,但不是标记上+ggplotly()在结束ggplot()调用,则需要围绕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)

虽然,我注意到的是, fill调用时,不尊重ggplotly ...

直接在为此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)))

见这里和这里更多的例子



文章来源: How to resolve ggplotly error using color fill
标签: r ggplot2 plotly