我在R中使用以下数据构建scatterplot
:
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
)
然后,我为每个类别创建一个dataframe
(一个用于&#39; A&#39;,&#39; B&#39;和&#39; C&#39;),因为我想要为每个类别创建一个下拉菜单。
我想用3种颜色为flag_presente
(&#39; Presente&#39;,#39; Ausente&#39;,&#39; MP&#39;)创建图例,但是当我添加时color
上的参数colors
和add_trace()
updatemenus
上的过滤无效。
这里是完整的代码:
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('Presente', 'Presente', 'Presente','Ausente', 'Ausente', 'MP','MP', 'Ausente', 'Presente', 'MP')
, stringsAsFactors = F
)
unq_categorias <- unique(df$categoria)
for (ctg in unq_categorias) {
assign(
paste0("df_", ctg),
subset(df, categoria==ctg)
)
}
# Retorna o número maximo de reviews
max_rev <- max(df$qtd_reviews) + 1
######
# Gráfico
p <- plot_ly() %>%
add_trace(data = df, y = ~nota, x = ~qtd_reviews, type = 'scatter', mode = 'markers', hoverinfo = 'text',
text = ~paste('Produto: ', produto,
'</br>Categoria: ', categoria,
'</br>Qtd Avaliações: ', round(qtd_reviews, 1),
'</br>Nota Média: ', round(nota, 1))) %>%
add_trace(data = df_A, y = ~nota, x = ~qtd_reviews, type = 'scatter', mode = 'markers', hoverinfo = 'text',
text = ~paste('Produto: ', produto,
'</br>Categoria: ', categoria,
'</br>Qtd Avaliações: ', round(qtd_reviews, 1),
'</br>Nota Média: ', round(nota, 1)),
visible = F) %>%
add_trace(data = df_B, y = ~nota, x = ~qtd_reviews, type = 'scatter', mode = 'markers', hoverinfo = 'text',
text = ~paste('Produto: ', produto,
'</br>Categoria: ', categoria,
'</br>Qtd Avaliações: ', round(qtd_reviews, 1),
'</br>Nota Média: ', round(nota, 1)),
visible = F) %>%
add_trace(data = df_C, y = ~nota, x = ~qtd_reviews, type = 'scatter', mode = 'markers', hoverinfo = 'text',
text = ~paste('Produto: ', produto,
'</br>Categoria: ', categoria,
'</br>Qtd Avaliações: ', round(qtd_reviews, 1),
'</br>Nota Média: ', round(nota, 1)),
visible = F) %>%
layout(
title = 'Título',
# Legend
legend = list(
x = 0.6, y = 1.1,
bordercolor = "#000000",
borderwidth = 1,
orientation = 'h',
font = list(
family = "sans-serif",
size = 12,
color = "#000")
),
# Axes
yaxis = list(
title='Nota Média',
zeroline = T,
rangemode = "tozero",
autorange = F,
range=list(0,5.1)
),
xaxis = list(
title='Quantidade de Avaliações',
zeroline = T,
rangemode = "tozero",
autorange = F,
range=list(0, max_rev)
),
updatemenus = list(
list(
y = 0.8,
buttons = list(
# Todas categorias
list(
method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE, FALSE)),
label = "Todas"
),
# Categoria A
list(
method = "restyle",
args = list("visible", list(FALSE, TRUE, FALSE, FALSE)),
label = "Categoria A"
),
# Categoria B
list(
method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE)),
label = "Categoria B"
),
# Categoria C
list(
method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, TRUE)),
label = "Categoria C"
)
)
)
)
) %>% config(displayModeBar = F)
ggplotly(p)
它是葡萄牙语,但理解代码并不是必需的。
有人可以帮忙吗?感谢
答案 0 :(得分:1)
通过在循环中添加每个categoria
来简化整个代码。然后你会得到“免费”的传奇。
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')
}
这也简化了您的updatemenus
。
完整,减少代码
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
答案 1 :(得分:0)
我得到它的方式(在Maximilian Peters回答之后),为每个类别添加了3条跟踪。每条跟踪代表SELECT Column1, EnumColumn, Column3, DateColumn, Column5
FROM ( SELECT Column1, EnumColumn, Column3, DateColumn, Column5
, ROW_NUMBER() OVER ( PARTITION BY EnumColumn
ORDER BY DateColumn DESC ) AS ROW_NUM
FROM MyTable
WHERE EnumColumn IN ('Foo', 'Bar', 'Baz')
) x
WHERE ROW_NUM = 1
的级别。
然后,对于每个按钮,我们需要一个包含9 flag_presente
和TRUE
的列表。
它是这样的:
FALSE
我们还需要注意图例是基于每个迹线,因此最好隐藏。