散点图颜色和图例基于标签上的R(plotly)
问题描述:
我中的R构建scatterplot
具有下列数据:散点图颜色和图例基于标签上的R(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
)
然后,创建用于每个类别的一个dataframe
(一个用于“A” ,'B'和'C'),因为我想为每个类别创建一个下拉菜单。
我想为flag_presente
创造传奇(“Presente”,“Ausente”,“MP”)有3种颜色,但是当我上添加add_trace()
参数color
和colors
上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)
其在葡萄牙,但它是没有必要的理解代码。
任何人都可以帮忙吗?谢谢
答
我得到了它(后Maximilian Peters答案),新增的3个痕迹每个类别的方式。每条痕迹代表flag_presente
的等级。
然后,对于每个按钮,我们需要列出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
我们还需要注意到,传说是基于每一个痕迹,所以它是更好地隐藏。
答
您可以通过在循环中添加每个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
我感到困惑,但这不是我想要的。我希望这个图例基于'flag_presente'。对不起,如果问题很混乱,我会尝试编辑以简化它。 – TheBiro