简体   繁体   English

如何在 R 中更改 ggplotly 中的图例位置

[英]How to change legend position in ggplotly in R

The below code generates two plots using ggplot and ggplotly .下面的代码使用ggplotggplotly生成两个图。 Despite of using the layout() to ggplotly the legend is still at the right side.尽管使用layout()来 ggplotly,但图例仍然在右侧。 The legend is required to be at the bottom.图例必须位于底部。 Could anyone help to move the legend to bottom in the ggplotly?任何人都可以帮助将图例移到 ggplotly 的底部吗? I have tried the solution at R + shiny + plotly: ggplotly moves the legend to the right and is not working here.我已经在R + Shiny + plotly尝试了解决方案:ggplotly 将图例向右移动,但在这里不起作用。 Can someone help if im missing the obvious.如果我错过了显而易见的事情,有人可以帮忙吗?

measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(measure,score)


ui <- fluidPage(
  mainPanel(
    plotOutput("myplot" ),
    plotlyOutput("myplot2" )
  )
)

server <- function(input, output) {
  myplot <- reactive({
    gpl1 <- ggplot(data,aes(y=reorder(measure, score),x=score,fill=score)) +
      geom_bar(stat="identity")+
      theme(legend.position="bottom")+
      xlab("x")+
      ylab("y")+
      labs(title = NULL)
    gpl1
  })
  
  myplot2 <- reactive({
    gpl2 <- ggplot(data,aes(y=reorder(measure, score),x=score,fill=score)) +
      geom_bar(stat="identity") +
      theme(legend.position="bottom")+
      xlab("x")+
      ylab("y")+
      labs(title = NULL)
    ggplotly(gpl2) %>% 
      layout(legend = list(orientation = 'h', x = 0.45, y = 1.1))
  })
  output$myplot <- renderPlot({
    myplot()
  })
  output$myplot2 <- renderPlotly({
    myplot2()
  })
}
  
shinyApp(ui = ui, server = server)

Okay this isn't a full answer, but it is a start that might help someone else figure it out, and it is too long to fit into a comment.好的,这不是一个完整的答案,但它是一个开始,可能会帮助其他人弄清楚,而且评论太长了。 It is possible it is worth opening up an issue, although I am not sure where (ie what package) the problem lies.有可能值得提出一个问题,尽管我不确定问题出在哪里(即哪个包)。

I re-ran the code from R + shiny + plotly: ggplotly moves the legend to the right and it works as it should, but for some reason yours doesn't.我重新运行了R + Shiny + plotly 中的代码:ggplotly 将图例向右移动,它按预期工作,但由于某种原因你的没有。 The only difference is that your fill is continuous, whereas the fill in that one is categorical.唯一的区别是您的fill是连续的,而那个填充是分类的。

If you add a categorical group here for score:如果您在此处添加分类组以获得分数:

measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(measure,score,score.f=as.factor(score))

Then the code you have works.然后您拥有的代码有效。 You need to modify the gpl2 object with fill=score.f (instead of fill=score ), and also need to change the y value in the layout to get the legend to the bottom:您需要使用fill=score.f (而不是fill=score )修改gpl2对象,并且还需要更改布局中的 y 值以使图例到达底部:

ggplotly(gpl2) %>% 
            layout(legend = list(orientation = 'h', x = 0.45, y = -.07))

在此处输入图片说明

Yet, as soon as you change the fill group to a continuous然而,一旦您将填充组更改为连续

score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(measure,score,score.f=score)

The legend goes back to being on the right:传说又回到了右边:

在此处输入图片说明

The problem here is, that you are dealing with a colorbar not a legend.这里的问题是,您正在处理颜色条而不是图例。

Currently plotly does not offer horizontal colorbars .目前 plotly不提供水平颜色条。

Also changing the position of a colorbar seems to be buggy in the ggplotly context:在 ggplotly 上下文中,更改颜色条的位置似乎也有问题:

library(shiny)
library(plotly)

measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(measure,score)


ui <- fluidPage(
  mainPanel(
    plotOutput("myplot" ),
    plotlyOutput("myplot2" )
  )
)

server <- function(input, output) {
  myplot <- reactive({
    gpl1 <- ggplot(data,aes(y=reorder(measure, score),x=score,fill=score)) +
      geom_bar(stat="identity")+
      theme(legend.position="bottom")+
      xlab("x")+
      ylab("y")+
      labs(title = NULL)
    gpl1
  })
  
  myplot2 <- reactive({
    fig <- ggplotly(myplot())
    fig <- style(fig, marker = list(colorbar = list(xanchor = "center", yanchor = "center", x = 0.5, y = -1, title = "test")), traces = NULL) # try traces = 1 or 1:2
    # browser()
    # plotly_json(fig)
  })
  
  output$myplot <- renderPlot({
    myplot()
  })
  
  output$myplot2 <- renderPlotly({
    myplot2()
  })
}

shinyApp(ui = ui, server = server)

Accordingly, for now I'd recommend not to modify the colorbar and use the defaults.因此,目前我建议不要修改颜色栏并使用默认值。

As @ismirsehregal mentioned plotly does not yet support the horizontal colorbar, and the colorbar is present to support the continuous-valued fill attribute.正如@ismirsehregal 提到的,plotly 尚不支持水平颜色条,并且存在颜色条以支持连续值填充属性。 So until plotly supports that orientation an alternative solution might serve temporarily: change the fill from continuous value to a discrete value.因此,在 plotly 支持该方向之前,可能会暂时使用替代解决方案:将填充从连续值更改为离散值。 Here is the reactive using (a) a cut for the discrete values, and (b) plotly anchors for the bottom legend.这是使用(a)离散值的切割和(b)底部图例的绘图锚点的反应。

myplot2 <- reactive({
  gpl2 <- ggplot(data,aes(y=reorder(measure, score),
                          x=score,
                          fill=cut(score, breaks=seq(0,800,100)))) +
      geom_bar(stat="identity") +
      labs(x='x',y='y',title=NULL,fill='Score') +
      theme(legend.position = 'bottom') +
      # scale_fill_stepsn(colours = terrain.colors(10),n.breaks=10)
      scale_fill_viridis_d()

    ggplotly(gpl2) %>%
      plotly::layout(legend=list(x=0, 
                                 xanchor='left',
                                 yanchor='bottom',
                                 orientation='h'))    
})

情节底部的传说

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM