[英]Change legend in plotly-R
I am quite new to plotly and am currently trying to create three different plotly bars for three indicators: Gini and Palma coefficients and S80/S20.我对 plotly 很陌生,目前正在尝试为三个指标创建三个不同的 plotly 条:基尼系数和帕尔马系数以及 S80/S20。 I have the following two problems:我有以下两个问题:
Thank you very much for any help.非常感谢您的帮助。 Complete code here:完整代码在这里:
library(plotly)
gini<-as.data.frame(c(0.4, 0.3, 0.2))
palma<-as.data.frame(c(1.2, 1.1, 1.0))
S<-as.data.frame(c(5.2, 5.1, 5.0))
p <- plot_ly(gini, x = "Coeff. di Gini", y = ~gini$`c(0.4, 0.3, 0.2)`, alpha = 0.7)
q<- plot_ly(palma, x="Coeff. di Palma", y=~palma$`c(1.2, 1.1, 1)`, alpha=0.7)
r<-plot_ly(S, x="S80/S20", y=~S$`c(5.2, 5.1, 5)`, alpha=0.7)
subplot(
add_trace(p, name = "Coeff. di Gini", x = "Coeff. di Gini", y = ~gini$`c(0.4, 0.3, 0.2)`, symbol = ~gini$`c(0.4, 0.3, 0.2)`, symbols = c('circle','x','o'),
marker = list(size = 10),type = 'scatter', mode = 'lines+markers'),
add_trace(q, name="Coeff. di Palma", x = "Coeff. di Palma", y = ~palma$`c(1.2, 1.1, 1)`, symbol = ~palma$`c(1.2, 1.1, 1)`, symbols = c('circle','x','o'),
marker = list(size = 10), type = 'scatter', mode = 'lines+markers'),
add_trace(r, name="S80/S20", x = "S80/S20", y = ~S$`c(5.2, 5.1, 5)`, symbol = ~S$`c(5.2, 5.1, 5)`, symbols = c('circle','x','o'),
marker = list(size = 10), type = 'scatter', mode = 'lines+markers')
)
the legend shows the name of the indicators however I would like it to show for the three different symbols three different years, let's say: circle=2018, x=2019, o=2020.图例显示了指标的名称,但我希望它显示三个不同年份的三个不同符号,比如说:circle=2018, x=2019, o=2020。 Is it possible to adjust it?可以调整吗?
You could add traces which only serve to provide a legend but no points are shown.您可以添加仅用于提供图例但不显示点的迹线。
visible = 'legendonly'
and hide the legend of your 'real' traces.并隐藏你的“真实”痕迹的传说。
showLegend = F
can the three plots appear with some distance between each other (say 1-2 cm of distance between graphs)?这三个图之间是否可以有一定的距离(比如图之间的距离为 1-2 厘米)?
If you add margin
to your subplot call you specify the distance between the subplots.如果您在子图调用中添加margin
,则可以指定子图之间的距离。 Needs a bit of fiddling but you could try需要一些摆弄,但你可以尝试
margin = 0.1
library(plotly)
gini <- as.data.frame(c(0.4, 0.3, 0.2))
palma <- as.data.frame(c(1.2, 1.1, 1.0))
S <- as.data.frame(c(5.2, 5.1, 5.0))
p <- plot_ly(x = "Coeff. di Gini",
y = gini$`c(0.4, 0.3, 0.2)`,
alpha = 0.7,
name = "Coeff. di Gini",
symbol = gini$`c(0.4, 0.3, 0.2)`,
symbols = c('circle','x','o'),
marker = list(size = 10),
type = 'scatter',
mode = 'lines+markers',
showlegend=F) %>%
add_markers(name = "2018",
marker = list(size = 10, symbol = 'circle'),
showlegend=T, visible='legendonly') %>%
add_markers(name = "2019",
marker = list(size = 10, symbol = 'x'),
showlegend=T, visible='legendonly') %>%
add_markers(name = "2020",
marker = list(size = 10, symbol = 'circle-open'),
showlegend=T, visible='legendonly')
q <- plot_ly(x="Coeff. di Palma",
y=palma$`c(1.2, 1.1, 1)`,
alpha=0.7,
name="Coeff. di Palma",
symbol = palma$`c(1.2, 1.1, 1)`,
symbols = c('circle','x', 'o'),
marker = list(size = 10),
type = 'scatter',
mode = 'lines+markers',
showlegend=F)
r <- plot_ly(x="S80/S20",
y=S$`c(5.2, 5.1, 5)`,
alpha=0.7,
name="S80/S20",
symbol = ~S$`c(5.2, 5.1, 5)`,
symbols = c('circle','x','o'),
marker = list(size = 10),
type = 'scatter',
mode = 'lines+markers',
showlegend=F)
subplot(
add_trace(p),
add_trace(q),
add_trace(r),
margin = 0.1
) %>% layout(showlegend=T)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.