简体   繁体   中英

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

I am trying to make a couple of plots (code below) and to keep the colors consistent between the two graphs. One graph contains 8 data points and the other contains 5. Despite using almost identical code for each of the plots, the colors do not match up. Does anyone have any insight as to why the colors are not matching up between the two graphs?

Correct colors ( 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))
  ))

Wrong colors ( 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))
  ))

Here are the data I am using for these plots:

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)
  )

And 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"
  )

The colors are different because you supplied different colors for each plot. More specifically because mkt533_product is a subset of mkt533 it retains the organization of each variable, even if all the contents of that variable aren't retained. So

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

and

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

have different content, but the same 8 levels. If you supply each plot with the same colors, the same color will be mapped to the same level in each plot, which is what you want

  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))
  ))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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