Plotly:如何根据值指定符号和颜色?

编程入门 行业动态 更新时间:2024-10-08 08:28:57
本文介绍了Plotly:如何根据值指定符号和颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 R 中使用 plotly 绘图时,如何根据值指定颜色和符号?例如对于 mtcars 示例数据集,如果 mtcars$mpg 大于或小于 18,如何绘制为红色方块?

When plotting with plotly in R, how does one specify a color and a symbol based on a value? For example with the mtcars example dataset, how to plot as a red square if mtcars$mpg is greater or less than 18?

例如:

library(plotly) p <- plot_ly(type = "scatter", data = mtcars, x = rownames(mtcars), y = mtcars$mpg, mode = "markers" )

如何将所有高于 20 的点设为黄色方块?

How to get all points above 20 as yellow squares?

推荐答案

你可以这样做:

plot_ly(type = "scatter", data = mtcars, x = rownames(mtcars), y = mtcars$mpg, mode = "markers", symbol = ~mpg > 20, symbols = c(16,15), color = ~mpg > 20, colors = c("blue", "yellow"))

plot.ly/r/line-and-scatter/#mapping-data-to-symbols

是的,这是可能的,我会首先使用 cut() 在 plot_ly() 之外进行所有分组和形状/颜色规范.然后在引用新的颜色和形状变量时利用 plot_ly() 中的文字 I() 语法:

yes it's possible, I'd make all of your grouping and shape/color specification outside of plot_ly() with cut() first. And then take advantage of the literal I() syntax inside of plot_ly() when referencing your new color and shape vars:

data(mtcars) mtcars$shape <- cut(mtcars$mpg, breaks = c(0,18, 26, 100), labels = c("square", "circle", "diamond")) mtcars$color <- cut(mtcars$mpg, breaks = c(0,18, 26, 100), labels = c("red", "yellow", "green")) plot_ly(type = "scatter", data = mtcars, x = rownames(mtcars), y = mtcars$mpg, mode = "markers", symbol = ~I(shape), color = ~I(color))

更多推荐

Plotly:如何根据值指定符号和颜色?

本文发布于:2023-07-19 13:44:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1157092.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号   颜色   Plotly

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!