如何在r中用绘图手动添加符号

发布于 2025-02-05 19:08:50 字数 1274 浏览 2 评论 0原文

我有一个使用三个轨迹的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.
three traceas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2025-02-12 19:08:50

一个选项是将列添加到您的数据框架中,然后可以在colorsymbol属性上映射。此外,我使用颜色符号的命名向量将颜色和符号分配给新列的类别。另请注意,颜色符号不应放在标记规范的list内。最后,我简化了您的代码。

“ triangle-down”符号不起作用。 nofollow noreferrer”>参考,仅(“ circle” |“ circle-open” |“ cross” |“ diamond” |“ diamond-open” |“ square” |“ square” |“ square-open” |“ x”)被接受。

library(plotly)

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, z = z1)
z2 <- z + 3
df2 <- data.frame(x, y, z = z2)

df$color <- "a"
df1$color <- "b"
df2$color <- "c"
symbols <- c("circle", "diamond", 'square')
colors <- c("gray", "lightgray", "darkslategray")
names(colors) <- names(symbols)  <- c("a", "b", "c")

plot<- plot_ly(x = ~x, y = ~y, z = ~z, color = ~color, symbol = ~color, colors = colors, symbols = symbols, marker = list(size = 8)) %>%
  add_trace(data = df, type = "scatter3d", mode = 'markers') %>%
  add_trace(data = df1, type = "scatter3d", mode = 'markers') %>%
  add_trace(data = df2, type = "scatter3d", mode = 'markers') %>%
  layout(title = 'Explore Options')
plot

One option would be to add a column to your dataframes which could then be mapped on the color and symbol attributes. Additionally I use named vectors of colors and symbols to assign colors and symbols to categories of the new column. Also note that colors and symbols should not to be placed inside the list 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.

library(plotly)

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, z = z1)
z2 <- z + 3
df2 <- data.frame(x, y, z = z2)

df$color <- "a"
df1$color <- "b"
df2$color <- "c"
symbols <- c("circle", "diamond", 'square')
colors <- c("gray", "lightgray", "darkslategray")
names(colors) <- names(symbols)  <- c("a", "b", "c")

plot<- plot_ly(x = ~x, y = ~y, z = ~z, color = ~color, symbol = ~color, colors = colors, symbols = symbols, marker = list(size = 8)) %>%
  add_trace(data = df, type = "scatter3d", mode = 'markers') %>%
  add_trace(data = df1, type = "scatter3d", mode = 'markers') %>%
  add_trace(data = df2, type = "scatter3d", mode = 'markers') %>%
  layout(title = 'Explore Options')
plot

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文