在 plotly-R 中更改图例

Change legend in plotly-R

我对 plotly 很陌生,目前正在尝试为三个指标创建三个不同的 plotly bar:Gini 和 Palma 系数以及 S80/S20。我有以下两个问题:

  1. 图例显示了指标的名称,但我希望它显示三个不同年份的三个不同符号,比方说:circle=2018,x=2019,o=2020。可以调整吗?
  2. 这三个图是否可以彼此之间有一定的距离(比如图形之间的距离为 1-2 厘米)?

非常感谢您的帮助。 完整代码在这里:

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. Is it possible to adjust it?

您可以添加仅用于提供图例但不显示任何点的轨迹。

visible = 'legendonly'

并隐藏您的 'real' 痕迹的图例。

showLegend = F

can the three plots appear with some distance between each other (say 1-2 cm of distance between graphs)?

如果将 margin 添加到子图调用中,则指定子图之间的距离。需要一些小技巧,但你可以试试

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)