You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Shiny中renderPlotly内ggplot图例位置设置失效问题

问题原因与解决方案

原因

你在ggplot2中设置的theme(legend.position = "top")是ggplot的主题配置,但当通过ggplotly()将ggplot对象转换为plotly交互图表时,plotly会使用自身的布局系统,默认覆盖ggplot的图例位置设置,将图例放在右侧,这是两者布局机制差异导致的。

解决方法

通过plotly的layout()函数手动指定图例的位置和排列方式,覆盖默认设置。以下是整合后的Shiny完整代码:

library(shiny)
library(ggplot2)
library(plotly)

temp <- structure(list(Cow_No = c(292, 258, 18, 103, 147, 224, 157, 241, 
230, 260, 305, 204, 310, 215, 192, 231, 304, 134, 24, 84, 92, 
46, 77, 4, 67, 238, 217, 49, 284, 168), core_product = c("Short Gestation Length", 
"PS Daughter Proven", "Short Gestation Length", "Short Gestation Length", 
"Short Gestation Length", "PS Daughter Proven", "Short Gestation Length", 
"Short Gestation Length", "PS Forward Pack", "Nominated", "Short Gestation Length", 
"PS Daughter Proven", "PS Daughter Proven", "PS Forward Pack", 
"PS Daughter Proven", "PS Daughter Proven", "PS Forward Pack", 
"PS Daughter Proven", "Nominated", "Short Gestation Length", 
"Short Gestation Length", "Short Gestation Length", "PS Forward Pack", 
"Nominated", "Short Gestation Length", "Short Gestation Length", 
"Nominated", "Nominated", "Short Gestation Length", "Nominated"
), bw = c(-31.1, -26.5, 21.3, 23, 35.9, 58.2, 63.3, 79.5, 83.9, 
83.9, 88.6, 108.9, 118.6, 119.4, 119.5, 119.6, 122.9, 123.7, 
128.8, 147.1, 149.4, 149.6, 154.4, 160.6, 167.7, 172.6, 173.4, 
181.5, 182.6, 184.3), seq = 1:30), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"))

ui <- fluidPage(
  plotlyOutput("barChart")
)

server <- function(input, output) {
  output$barChart <- renderPlotly({
    allprod_chart <- ggplot(temp, aes(x=seq, y = bw, fill=core_product,label=Cow_No)) +
      geom_bar(stat="identity") +
      ggtitle("ABC") +
      scale_fill_manual(values=c( "PS Forward Pack" ="deepskyblue", "Short Gestation Length" ="brown",
                                  "Sire Proving Scheme" = "blue3", "PS Daughter Proven" = "darkblue",
                                  "Nominated"= "brown3",  "Sexed Semen"  = "darkgreen" ,
                                  "PS A2A2" = "lightblue", "Frozen Sexed"  = "lightgreen",
                                  "Unknown" = "grey"
      ))+
      theme_minimal() +
      theme( axis.ticks.x=element_blank()
             ,axis.title = element_text( size = 14, face = "bold" )
             , legend.position = "top"
             , strip.text = element_text(size = 14, face = "bold")
             , plot.title = element_text(size = 20)
      )
    
    # 转换为plotly并调整图例位置
    ggplotly(allprod_chart) %>%
      layout(
        legend = list(
          orientation = "h",  # 图例水平排列
          x = 0.5, y = 1.1,   # 位置:水平居中,顶部外侧
          xanchor = "center"  # 锚点居中,确保图例对齐
        )
      )
  })
}

shinyApp(ui, server)

参数说明

  • orientation = "h":将图例从默认垂直排列改为水平排列,适配顶部布局
  • x = 0.5 和 xanchor = "center":让图例在图表宽度范围内居中
  • y = 1.1:将图例放置在图表主体上方,避免与柱状图内容重叠

内容的提问来源于stack exchange,提问作者Ann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 05:58:11