向条形图添加额外的网格线会使图例的名称加倍 [英] Adding extra gridlines to bar chart doubles the names of the legend

查看:49
本文介绍了向条形图添加额外的网格线会使图例的名称加倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了下面的条形图,在其中我在主 gridlines 之间添加了 gridlines.问题是这个新的 trace 使我不想要的 legend 的名称翻倍.

I create the bar chart below in which I add gridlines between the main gridlines. The issue is that this new trace doubles the names of my legend which I do not want to.

co<-c("AL","FG","GH","HJ","FT")
va<-c(1000000,1000000,1000000,5000000,5000000)
cova<-data.frame(co,va)
mtick <- 2*10**5 # minor ticks every 200k
library(plotly)
library(comprehenr)
    
    fig1 <- plot_ly(cova, x = ~co, y = ~va,
                    type = 'bar',
                    hovertemplate = paste('%{x}', '<br>Deaths: %{y}<br><extra></extra>'),
                    colors = c("#60ab3d","#6bbabf","#c4d436","#3e5b84","#028c75"),
                    color = ~co
                    
                    
                    
    ) 
    fig1 <- fig1 %>% layout(showlegend = TRUE,title=list(text="worldwide,by selected territories and period",x = 0,y=1,font=list(size=10)),
                            font = list(color = '#a2a2a2'),
                            legend=list(title=list(text='<b> Top 5 </b>'),itemclick = F, itemdoubleclick = F,orientation="h",xanchor = "center",x = 0,y=1.1),
                            
                            yaxis = list(fixedrange = TRUE,title="",
                                         #dtick = 250000
                                         showgrid = T,gridcolor = "#a2a2a2", showline = FALSE, showticklabels = TRUE, domain= c(0, 0.85)),
                            xaxis = list(fixedrange = TRUE,title="",zeroline = FALSE, showline = T,showticklabels = F,tickangle=45, showgrid = FALSE))%>% 
      
      config(modeBarButtonsToRemove = c('toImage',"zoom2d","toggleSpikelines","hoverClosestCartesian","hoverCompareCartesian","drawline","autoScale2d" ,"resetScale2d","zoomIn2d","zoomOut2d","pan2d",'select2d','lasso2d'))%>%
      config(displaylogo = FALSE)
    fig1%>% add_trace(x=~co, y=~va, yaxis="y2", marker=list(color='rgba(0,0,0,0)')) %>%
      layout(yaxis2=list( overlaying="y", 
                          tickvals=to_list(for(i in seq(0, max(cova$va), by=mtick) )  if (i%%10**4) i ), 
                          showticklabels=FALSE ,showlegend=F))

推荐答案

add_trace中添加showlegend=F -

fig1 %>% 
  add_trace(x=~co, y=~va, yaxis="y2", showlegend=F) %>%
  layout(yaxis2=list( overlaying="y", 
                      tickvals=to_list(for(i in seq(0, max(cova$va), by=mtick) )  if (i%%10**4) i ), 
                      showticklabels=FALSE ,showlegend=F))

这篇关于向条形图添加额外的网格线会使图例的名称加倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆