如何在r中用绘图手动添加符号
我有一个使用三个轨迹的plot_ly 3D散点图,我想为每个迹线使用不同的符号和颜色。我的代码看起来像这样
library(plotrix)
library(plotly)
library(processx)
x <- c(1, 2, 3, 4)
y <- c(2, 4, 6, 8)
z <- c(1, 2, 3, 4)
df <- data.frame(x, y, z)
z1 <- z + 1.5
df1 <- data.frame(x, y, z1)
z2 <- z + 3
df2 <- data.frame(x, y, z2)
symbols <- c("circle", "diamond", 'triangle-down')
colors <- c("gray", "lightgray", "darkslategray")
plot<- plot_ly()%>%
add_trace(data = df, x = ~x, y = ~y, z = ~z,type = "scatter3d",
mode = 'markers', marker = list(size = 8, symbol = 1, symbols = symbols, color = 1, colors = colors)) %>%
add_trace(data = df1, x = ~x, y = ~y, z = ~z1,type = "scatter3d",
mode = 'markers', marker = list(size = 8, symbol = 2, symbols = symbols, color = 2, colors = colors)) %>%
add_trace(data = df2, x = ~x, y = ~y, z = ~z2,type = "scatter3d",
mode = 'markers', marker = list(size = 8, symbol = 8, symbols = symbols, color = 3, colors = colors)) %>%
layout(title = 'Explore Options')
plot
,我想拥有第一个使用圆形标记,第二颗钻石和第三个三角形的痕迹,每个钻石和第三个三角形的颜色不同,但我只是得到彩色圆圈,即
I have a plot_ly 3D scatter plot that uses three traces, and I want to use a different symbol and color for each trace. My code looks like this
library(plotrix)
library(plotly)
library(processx)
x <- c(1, 2, 3, 4)
y <- c(2, 4, 6, 8)
z <- c(1, 2, 3, 4)
df <- data.frame(x, y, z)
z1 <- z + 1.5
df1 <- data.frame(x, y, z1)
z2 <- z + 3
df2 <- data.frame(x, y, z2)
symbols <- c("circle", "diamond", 'triangle-down')
colors <- c("gray", "lightgray", "darkslategray")
plot<- plot_ly()%>%
add_trace(data = df, x = ~x, y = ~y, z = ~z,type = "scatter3d",
mode = 'markers', marker = list(size = 8, symbol = 1, symbols = symbols, color = 1, colors = colors)) %>%
add_trace(data = df1, x = ~x, y = ~y, z = ~z1,type = "scatter3d",
mode = 'markers', marker = list(size = 8, symbol = 2, symbols = symbols, color = 2, colors = colors)) %>%
add_trace(data = df2, x = ~x, y = ~y, z = ~z2,type = "scatter3d",
mode = 'markers', marker = list(size = 8, symbol = 8, symbols = symbols, color = 3, colors = colors)) %>%
layout(title = 'Explore Options')
plot
which I would like to have the first trace to use circle markers, the second diamond and the third triangles, each with a different gray scale color, but instead I just get colored circles i.e.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个选项是将列添加到您的数据框架中,然后可以在
color
和symbol
属性上映射。此外,我使用颜色
和符号
的命名向量将颜色和符号分配给新列的类别。另请注意,颜色
和符号
不应放在标记规范的list
内。最后,我简化了您的代码。“ triangle-down”
符号不起作用。 nofollow noreferrer”>参考,仅(“ circle” |“ circle-open” |“ cross” |“ diamond” |“ diamond-open” |“ square” |“ square” |“ square-open” |“ x”)被接受。One option would be to add a column to your dataframes which could then be mapped on the
color
andsymbol
attributes. Additionally I use named vectors ofcolors
andsymbols
to assign colors and symbols to categories of the new column. Also note thatcolors
andsymbols
should not to be placed inside thelist
for the marker specifications. Finally I simplified your code a bit.The
"triangle-down"
symbol does not work, according to this reference, only ( "circle" | "circle-open" | "cross" | "diamond" | "diamond-open" | "square" | "square-open" | "x" ) are accepted.