繁体   English   中英

颜色在 3D 散点图 (R) 上显示不正确

[英]Colors not displaying correctly on a 3D scatterplot (R)

我正在尝试绘制几个图(下面的代码)并保持两个图形之间的颜色一致。 一张图包含 8 个数据点,另一张图包含 5 个数据点。尽管每个图使用几乎相同的代码,但颜色并不匹配。 有没有人知道为什么两个图表之间的颜色不匹配?

正确的颜色( https://plot.ly/~MKT533/3/#/ ):

p <-
  plot_ly(
    mkt533,
    x = ~ onsitetrainers_x,
    y = ~ lowprice_y,
    z = ~ flexibleclasses_z,
    color = ~ name,
    colors = c(
      "#AB1100",
      "#00274C",
      "#00B5AF",
      "#00274C",
      "#00274C",
      "#72088E",
      "#E9B000",
      "#0050AC"
    )
  ) %>%
  add_markers() %>%
  layout(scene = list(
    xaxis = list(title =  "Professional guidance",
                 range = c(1, 10)),
    yaxis = list(title =
                   "Value for money", range = c(1, 10)),
    zaxis = list(title =
                   "Time flexibility", range = c(1, 10))
  ))

错误的颜色( https://plot.ly/~MKT533/1/#/ ):

mkt533_product <- subset(mkt533, type!="Segment")

product <-
  plot_ly(
    mkt533_product,
    x = ~ onsitetrainers_x,
    y = ~ lowprice_y,
    z = ~ flexibleclasses_z,
    color = ~ name,
    colors = c("#AB1100", "#00B5AF", "#72088E", "#E9B000", "#0050AC")
  ) %>%
  add_markers() %>%
  layout(scene = list(
    xaxis = list(title = "Professional guidance", range = c(1, 10)),
    yaxis = list(title = "Value for money", range =
                   c(1, 10)),
    zaxis = list(title = "Time flexibility", range =
                   c(1, 10))
  ))

以下是我用于这些图的数据:

mkt533 <-
  structure(
    list(
      onsitetrainers_x = c(1L, 3L, 10L, 9L, 2L, 1L,
                           7L, 10L),
      lowprice_y = c(10L, 3L, 3L, 2L, 7L, 7L, 3L, 1L),
      flexibleclasses_z = c(4L,
                            8L, 3L, 5L, 7L, 1L, 6L, 6L),
      name = structure(
        c(4L, 2L, 5L, 3L,
          7L, 1L, 8L, 6L),
        .Label = c(
          "At-home gym",
          "Busy young families",
          "CrossFit",
          "Fitness-conscious youth",
          "Need that extra push",
          "Taekwondo gym",
          "YMCA",
          "Yoga studio"
        ),
        class = "factor"
      ),
      type = structure(
        c(3L,
          3L, 3L, 2L, 5L, 1L, 6L, 4L),
        .Label = c(
          "At-home gym",
          "CrossFit",
          "Segment",
          "Taekwondo gym",
          "YMCA",
          "Yoga studio"
        ),
        class = "factor"
      ),
      size = c(0.55, 0.3, 0.15, 0.25, 0.25, 0.25, 0.25, 0.25)
    ),
    class = "data.frame",
    row.names = c(NA,-8L)
  )

和 mkt533_product

structure(
    list(
      onsitetrainers_x = c(9L, 2L, 1L, 7L, 10L),
      lowprice_y = c(2L,
                     7L, 7L, 3L, 1L),
      flexibleclasses_z = c(5L, 7L, 1L, 6L, 6L),
      name = structure(
        c(3L,
          7L, 1L, 8L, 6L),
        .Label = c(
          "At-home gym",
          "Busy young families",
          "CrossFit",
          "Fitness-conscious youth",
          "Need that extra push",
          "Taekwondo gym",
          "YMCA",
          "Yoga studio"
        ),
        class = "factor"
      ),
      type = structure(
        c(2L,
          5L, 1L, 6L, 4L),
        .Label = c(
          "At-home gym",
          "CrossFit",
          "Segment",
          "Taekwondo gym",
          "YMCA",
          "Yoga studio"
        ),
        class = "factor"
      ),
      size = c(0.25,
               0.25, 0.25, 0.25, 0.25)
    ),
    row.names = 4:8,
    class = "data.frame"
  )

颜色不同是因为您为每个图提供了不同的颜色。 更具体地讲,因为mkt533_product是一个subsetmkt533它保留了每个变量的组织,即使该变量的所有内容都不会保留。 所以

mkt533$name
[1] Fitness-conscious youth Busy young families     Need that extra push    CrossFit               
[5] YMCA                    At-home gym             Yoga studio             Taekwondo gym          
8 Levels: At-home gym Busy young families CrossFit Fitness-conscious youth Need that extra push Taekwondo gym ... Yoga studio

mkt533_product$name
[1] CrossFit      YMCA          At-home gym   Yoga studio   Taekwondo gym
8 Levels: At-home gym Busy young families CrossFit Fitness-conscious youth Need that extra push Taekwondo gym ... Yoga studio

有不同的内容,但相同的8个级别。 如果您为每个图提供相同的颜色,则相同的颜色将映射到每个图中的相同级别,这就是您想要的

  p <-
  plot_ly(
    mkt533,
    x = ~ onsitetrainers_x,
    y = ~ lowprice_y,
    z = ~ flexibleclasses_z,
    color = ~ name,
    colors = c(
      "#AB1100",
      "#00274C",
      "#00B5AF",
      "#00274C",
      "#00274C",
      "#72088E",
      "#E9B000",
      "#0050AC"
    )
  ) %>%
  add_markers() %>%
  layout(scene = list(
    xaxis = list(title = "Professional guidance", range = c(1, 10)),
    yaxis = list(title = "Value for money", range =
                   c(1, 10)),
    zaxis = list(title = "Time flexibility", range =
                   c(1, 10))
  ))

在此处输入图片说明

product <-
  plot_ly(
    mkt533_product,
    x = ~ onsitetrainers_x,
    y = ~ lowprice_y,
    z = ~ flexibleclasses_z,
    color = ~ name,
    colors = c(
      "#AB1100",
      "#00274C",
      "#00B5AF",
      "#00274C",
      "#00274C",
      "#72088E",
      "#E9B000",
      "#0050AC"
    )
  ) %>%
  add_markers() %>%
  layout(scene = list(
    xaxis = list(title = "Professional guidance", range = c(1, 10)),
    yaxis = list(title = "Value for money", range =
                   c(1, 10)),
    zaxis = list(title = "Time flexibility", range =
                   c(1, 10))
  ))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM