本文介绍了如何使用颜色填充来解决ggplotly错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用从ggplot2扩展的 ggplotly 调用,我试图给点添加大纲。 对 ggplot 的调用有效,但将调用扩展为 ggplotly 会产生错误。 最小可重现的例子: 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]中要替换的项目数不是替换长度的倍数 解决方案您几乎在那里,而不是在 ggplot() + ggplotly() $ c> call,你需要用 ggplotly $ b $>包围 ggplot b ##保存ggplot对象g geom_point pg = I(21))+ scale_fill_continuous(low ='red',high ='blue') ##在ggplot对象上调用ggplotly ggplotly(g) 虽然我注意到 fill 在调用 ggplotly ...时不受尊重... 要直接在 plot_ly 中执行此操作,您可以指定一个调色板并将其绘制出来 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.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 plotIn 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 objectggplotly(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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 15:45