基于R中标签的散点打印颜色和图例(打印)

基于R中标签的散点打印颜色和图例(打印),r,plotly,R,Plotly,我正在用以下数据在R中构建一个散点图: df <- data.frame( produto=c('P1','P2','P3','P4','P5','P6','P7','P8','P9','P10') , categoria=c('A','B','C','A','B','A','B','C','A','C') , qtd_reviews=runif(10,10,50) , nota=runif(10,0,5) , flag_presente=c('Presente', '

我正在用以下数据在R中构建一个
散点图

df <- data.frame(
  produto=c('P1','P2','P3','P4','P5','P6','P7','P8','P9','P10')
  , categoria=c('A','B','C','A','B','A','B','C','A','C')
  , qtd_reviews=runif(10,10,50)
  , nota=runif(10,0,5)
  , flag_presente=c('Presente', 'Presente', 'Presente','Ausente', 'Ausente', 'MP','MP', 'Ausente', 'Presente', 'MP')
  , stringsAsFactors = F
)
它是葡萄牙语的,但它不是理解代码所必需的


有人能帮忙吗?谢谢

您通过在循环中添加每个
categoria
简化了整个代码。然后,您将获得“免费”的图例

我得到它的方式(在回答之后),是为每个类别添加3个痕迹。每个轨迹都表示在显示的
标志的级别上

然后,对于每个按钮,我们需要一个包含9个
TRUE
FALSE
的列表

事情是这样的:

library("plotly")

df <- data.frame(produto=c('P1','P2','P3','P4','P5','P6','P7','P8','P9','P10'),
                 categoria=c('A','B','C','A','B','A','B','C','A','C'),
                 qtd_reviews=runif(10,10,50),
                 nota=runif(10,0,5),
                 flag_presente=c('P', 'P', 'P','A', 'A', 'MP','MP', 'A', 'P', 'MP'),
                 stringsAsFactors = F
)

p <- plot_ly()
for (i in unique(df$categoria)) {

  # P
  plot_df <- df[(df$categoria==i) & (df$flag_presente=='P'),]
  p <- add_trace(p, 
                 data = plot_df, 
                 y = ~nota, 
                 x = ~qtd_reviews, 
                 type = 'scatter', 
                 mode = 'markers',
                 marker = list(color = "red"))

  # A
  plot_df <- df[(df$categoria==i) & (df$flag_presente=='A'),]
  p <- add_trace(p, 
                 data = plot_df, 
                 y = ~nota, 
                 x = ~qtd_reviews, 
                 type = 'scatter', 
                 mode = 'markers',
                 marker = list(color = "green"))

  # MP
  plot_df <- df[(df$categoria==i) & (df$flag_presente=='MP'),]
  p <- add_trace(p, 
                 data = plot_df, 
                 y = ~nota, 
                 x = ~qtd_reviews, 
                 type = 'scatter', 
                 mode = 'markers',
                 marker = list(color = "blue"))
}


p <- layout(p, showlegend = F,

            updatemenus = list(
              list(
                y = 0.8,
                buttons = list(
                  list(
                    method = "restyle",
                    args = list("visible", list(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)),
                    label = "All"
                  ),

                  list(
                    method = "restyle",
                    args = list("visible", list(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)),
                    label = "Category A"
                  ),

                  list(
                    method = "restyle",
                    args = list("visible", list(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE)),
                    label = "Category B"
                  ),

                  list(
                    method = "restyle",
                    args = list("visible", list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE)),
                    label = "Category C"
                  )
                )
              )
            )
)

p
library(“plotly”)

我很困惑,但这不是我想要的。我希望图例基于演示的
标志
。很抱歉,如果这个问题让人困惑,我将尝试编辑以简化它。
p <- plot_ly()
for (i in unique(df$categoria)) {
  p <- add_trace(p, 
                 data = df[df$categoria==i,], 
                 y = ~nota, 
                 x = ~qtd_reviews, 
                 type = 'scatter', 
                 mode = 'markers')
}
library("plotly")

df <- data.frame(produto=c('P1','P2','P3','P4','P5','P6','P7','P8','P9','P10'),
                 categoria=c('A','B','C','A','B','A','B','C','A','C'),
                 qtd_reviews=runif(10,10,50),
                 nota=runif(10,0,5),
                 stringsAsFactors = F
)

p <- plot_ly()
for (i in unique(df$categoria)) {
  p <- add_trace(p, 
                 data = df[df$categoria==i,], 
                 y = ~nota, 
                 x = ~qtd_reviews, 
                 type = 'scatter', 
                 mode = 'markers')
}


p <- layout(p, 
  updatemenus = list(
    list(
      y = 0.8,
      buttons = list(
        list(
          method = "restyle",
          args = list("visible", list(TRUE, TRUE, TRUE)),
          label = "All"
        ),

        list(
          method = "restyle",
          args = list("visible", list(TRUE, FALSE, FALSE)),
          label = "Category A"
        ),

        list(
          method = "restyle",
          args = list("visible", list(FALSE, TRUE, FALSE)),
          label = "Category B"
        ),

        list(
          method = "restyle",
          args = list("visible", list(FALSE, FALSE, TRUE)),
          label = "Category C"
        )
      )
    )
  )
)

p
library("plotly")

df <- data.frame(produto=c('P1','P2','P3','P4','P5','P6','P7','P8','P9','P10'),
                 categoria=c('A','B','C','A','B','A','B','C','A','C'),
                 qtd_reviews=runif(10,10,50),
                 nota=runif(10,0,5),
                 flag_presente=c('P', 'P', 'P','A', 'A', 'MP','MP', 'A', 'P', 'MP'),
                 stringsAsFactors = F
)

p <- plot_ly()
for (i in unique(df$categoria)) {

  # P
  plot_df <- df[(df$categoria==i) & (df$flag_presente=='P'),]
  p <- add_trace(p, 
                 data = plot_df, 
                 y = ~nota, 
                 x = ~qtd_reviews, 
                 type = 'scatter', 
                 mode = 'markers',
                 marker = list(color = "red"))

  # A
  plot_df <- df[(df$categoria==i) & (df$flag_presente=='A'),]
  p <- add_trace(p, 
                 data = plot_df, 
                 y = ~nota, 
                 x = ~qtd_reviews, 
                 type = 'scatter', 
                 mode = 'markers',
                 marker = list(color = "green"))

  # MP
  plot_df <- df[(df$categoria==i) & (df$flag_presente=='MP'),]
  p <- add_trace(p, 
                 data = plot_df, 
                 y = ~nota, 
                 x = ~qtd_reviews, 
                 type = 'scatter', 
                 mode = 'markers',
                 marker = list(color = "blue"))
}


p <- layout(p, showlegend = F,

            updatemenus = list(
              list(
                y = 0.8,
                buttons = list(
                  list(
                    method = "restyle",
                    args = list("visible", list(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)),
                    label = "All"
                  ),

                  list(
                    method = "restyle",
                    args = list("visible", list(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)),
                    label = "Category A"
                  ),

                  list(
                    method = "restyle",
                    args = list("visible", list(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE)),
                    label = "Category B"
                  ),

                  list(
                    method = "restyle",
                    args = list("visible", list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE)),
                    label = "Category C"
                  )
                )
              )
            )
)

p