如何使用颜色填充来解决ggplotly错误 [英] How to resolve ggplotly error using color fill

查看:309
本文介绍了如何使用颜色填充来解决ggplotly错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用从ggplot2扩展的 ggplotly 调用,我试图给点添加大纲。



ggplot 的调用有效,但将调用扩展为 ggplotly 会产生错误。


目标是基于具有黑色轮廓的连续变量(使用 pch

最小可重现的例子:

  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_f ill_continuous(低='红',高='蓝')+ ggplotly()

错误:不知道如何将o添加到图中
另外:警告消息:
在L $ marker $ color [idx]中< - aes2plotly(data,params,fill )[idx]:
要替换的项目数不是替换长度的倍数

解决方案

您几乎在那里,而不是在 ggplot() + ggplotly() $ c> call,你需要用 ggplotly $ b $>包围 ggplot b

  ##保存ggplot对象
g < - ggplot(data = mtcars,aes(mpg,wt,fill = hp))+
geom_point pg = I(21))+
scale_fill_continuous(low ='red',high ='blue')

##在ggplot对象上调用ggplotly
ggplotly(g)

虽然我注意到 fill 在调用 ggplotly ...时不受尊重...



要直接在 plot_ly 中执行此操作,您可以指定一个调色板并将其绘制出来



<$ p $ (长度(独特的(mtcars $ hp)))

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

请参阅


Using the ggplotly call extending from ggplot2, I'm trying to add an outline to the points.

The call to ggplot works, but extending the call to ggplotly yields an error.

The objective is the have an interactive plot with color filled points based on a continuous variable with a black outline (using pch=21 in this case).

Minimal reproducible example:

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

解决方案

You're almost there, but instead of tagging on +ggplotly() on the end of the ggplot() call, you need to surround the ggplot with ggplotly

## 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)

Although, I am noticing that the fill isn't respected when calling ggplotly...

To do this directly in plot_ly, you can specify a colour palette and plot it

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)))

See here and here for more examples

这篇关于如何使用颜色填充来解决ggplotly错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆