繁体   English   中英

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

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

下面的代码使用ggplotggplotly生成两个图。 尽管使用layout()来 ggplotly,但图例仍然在右侧。 图例必须位于底部。 任何人都可以帮助将图例移到 ggplotly 的底部吗? 我已经在R + Shiny + plotly尝试了解决方案:ggplotly 将图例向右移动,但在这里不起作用。 如果我错过了显而易见的事情,有人可以帮忙吗?

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)

好的,这不是一个完整的答案,但它是一个开始,可能会帮助其他人弄清楚,而且评论太长了。 有可能值得提出一个问题,尽管我不确定问题出在哪里(即哪个包)。

我重新运行了R + Shiny + plotly 中的代码:ggplotly 将图例向右移动,它按预期工作,但由于某种原因你的没有。 唯一的区别是您的fill是连续的,而那个填充是分类的。

如果您在此处添加分类组以获得分数:

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))

然后您拥有的代码有效。 您需要使用fill=score.f (而不是fill=score )修改gpl2对象,并且还需要更改布局中的 y 值以使图例到达底部:

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

在此处输入图片说明

然而,一旦您将填充组更改为连续

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

传说又回到了右边:

在此处输入图片说明

这里的问题是,您正在处理颜色条而不是图例。

目前 plotly不提供水平颜色条。

在 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)

因此,目前我建议不要修改颜色栏并使用默认值。

正如@ismirsehregal 提到的,plotly 尚不支持水平颜色条,并且存在颜色条以支持连续值填充属性。 因此,在 plotly 支持该方向之前,可能会暂时使用替代解决方案:将填充从连续值更改为离散值。 这是使用(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