如何使用 plotly 在散点图中显示第三个变量的第二个图例?
How to show a second legend for a third variable in a scatter plot using plotly?
我有以下代码生成散点图(使用 plotly in r)and I would like to add the legend 表示标记的大小 totalValue
(它是一个连续变量,表示特定建筑物的价值),我该怎么做那?
f <- list(
family = "Courier New, monospace",
size = 18,
color = "#7f7f7f"
)
x <- list(
title = "Age of Buildings",
titlefont = f,
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE,
showgrid = TRUE
)
y <- list(
title = "Total Violations",
titlefont = f,
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE,
showgrid = TRUE
)
fig2 <- plot_ly(final, x=~agebuilding, y=~violationstotal, mode= "markers", color =
~INdexrehabless6, size = ~totalvalue)
fig2 <- fig2 %>% layout(xaxis = x, yaxis = y, legend=list(title=list(text='<b>
Housing Conditions </b>'))) #chaging name legend
fig2
数据集样本:
agebuilding violationstotal INdexrehabless6 totalvalue
32 5 0 350000
120 15 1 50000
100 25 1 100000
32 31 0 210000
33 9 0 150000
50 20 0 301000
15 28 0 175000
70 18 1 125000
这是我得到的情节
据我所知,尚无法在 plotly 中绘制两个图例:
即使使用 ggplot(高度可定制)和 ggplotly(plotly 的包装),最终也只会导致一个图例.
library(ggplot2); library(dplyr); library(magrittr);
final <- read.table(header = T, text =
"agebuilding violationstotal INdexrehabless6 totalvalue
32 5 0 350000
120 15 1 50000
100 25 1 100000
32 31 0 210000
33 9 0 150000
50 20 0 301000
15 28 0 175000
70 18 1 125000");
final;
agebuilding violationstotal INdexrehabless6 totalvalue
1 32 5 0 350000
2 120 15 1 50000
3 100 25 1 100000
4 32 31 0 210000
5 33 9 0 150000
6 50 20 0 301000
7 15 28 0 175000
8 70 18 1 125000
#plots the ggplot in `plot` window. 1st GRAPH
ggplot(data = final, aes(x = agebuilding, y = violationstotal)) +
geom_point(aes(size = totalvalue, color = INdexrehabless6))
# plots the ggploty in viewer window. 2nd GRAPH (interactive)
ggp %>% ggplotly();
第一个图表
第二个图表
我有以下代码生成散点图(使用 plotly in r)and I would like to add the legend 表示标记的大小 totalValue
(它是一个连续变量,表示特定建筑物的价值),我该怎么做那?
f <- list(
family = "Courier New, monospace",
size = 18,
color = "#7f7f7f"
)
x <- list(
title = "Age of Buildings",
titlefont = f,
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE,
showgrid = TRUE
)
y <- list(
title = "Total Violations",
titlefont = f,
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE,
showgrid = TRUE
)
fig2 <- plot_ly(final, x=~agebuilding, y=~violationstotal, mode= "markers", color =
~INdexrehabless6, size = ~totalvalue)
fig2 <- fig2 %>% layout(xaxis = x, yaxis = y, legend=list(title=list(text='<b>
Housing Conditions </b>'))) #chaging name legend
fig2
数据集样本:
agebuilding violationstotal INdexrehabless6 totalvalue
32 5 0 350000
120 15 1 50000
100 25 1 100000
32 31 0 210000
33 9 0 150000
50 20 0 301000
15 28 0 175000
70 18 1 125000
这是我得到的情节
据我所知,尚无法在 plotly 中绘制两个图例:
即使使用 ggplot(高度可定制)和 ggplotly(plotly 的包装),最终也只会导致一个图例.
library(ggplot2); library(dplyr); library(magrittr);
final <- read.table(header = T, text =
"agebuilding violationstotal INdexrehabless6 totalvalue
32 5 0 350000
120 15 1 50000
100 25 1 100000
32 31 0 210000
33 9 0 150000
50 20 0 301000
15 28 0 175000
70 18 1 125000");
final;
agebuilding violationstotal INdexrehabless6 totalvalue
1 32 5 0 350000
2 120 15 1 50000
3 100 25 1 100000
4 32 31 0 210000
5 33 9 0 150000
6 50 20 0 301000
7 15 28 0 175000
8 70 18 1 125000
#plots the ggplot in `plot` window. 1st GRAPH
ggplot(data = final, aes(x = agebuilding, y = violationstotal)) +
geom_point(aes(size = totalvalue, color = INdexrehabless6))
# plots the ggploty in viewer window. 2nd GRAPH (interactive)
ggp %>% ggplotly();
第一个图表
第二个图表