gpt4 book ai didi

r - R Shiny 的中心 plotly 图

转载 作者:行者123 更新时间:2023-12-04 12:18:44 25 4
gpt4 key购买 nike

我希望以下应用程序中的 3 个圆环图在它们的 div 中居中,但它们都是右对齐的。当更改浏览器窗口的宽度(macOS 上的 Safari)时,它们会按照我希望的方式居中,但在页面最初加载时则不会。

任何想法如何始终将 3 个图表始终居中显示在它们下方显示的标题上方?

library(shiny)
library(plotly)

# Define UI for application
ui <- navbarPage("Home",
tabPanel("Tab1",
tags$style(type="text/css",
"h3 { text-align:center; }"
),
fluidRow(
column(4,
h2(plotlyOutput("plot1", height = "100%")),
h3("Score")
),
column(4,
h2(plotlyOutput("plot2", height = "100%")),
h3("Score")
),
column(4,
h2(plotlyOutput("plot3", height = "100%")),
h3("Score")
)
)
)
)

# Define server logic
server <- function(input, output) {

output$plot1 <- renderPlotly({

iScore <- sample(70:100, 1)

plot_ly(width = 150, height = 150,
marker = list(colors = c("#65b146", "rgba(0,0,0,0)"))) %>%
add_pie(values = c(iScore, 100 - iScore),
hole = .7,
textposition = 'none') %>%
layout(autosize = TRUE,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
showlegend = FALSE,
annotations = list(text = paste0("<b>",iScore,"</b>"),
align = "center",
font = list(size = 24),
showarrow = FALSE)) %>%
config(displayModeBar = F)
})

output$plot2 <- renderPlotly({

iScore <- sample(70:100, 1)

plot_ly(width = 150, height = 150,
marker = list(colors = c("#65b146", "rgba(0,0,0,0)"))) %>%
add_pie(values = c(iScore, 100 - iScore),
hole = .7,
textposition = 'none') %>%
layout(autosize = TRUE,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
showlegend = FALSE,
annotations = list(text = paste0("<b>",iScore,"</b>"),
align = "center",
font = list(size = 24),
showarrow = FALSE)) %>%
config(displayModeBar = F)
})

output$plot3 <- renderPlotly({

iScore <- sample(70:100, 1)

plot_ly(width = 150, height = 150,
marker = list(colors = c("#65b146", "rgba(0,0,0,0)"))) %>%
add_pie(values = c(iScore, 100 - iScore),
hole = .7,
textposition = 'none') %>%
layout(autosize = TRUE,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
showlegend = FALSE,
annotations = list(text = paste0("<b>",iScore,"</b>"),
align = "center",
font = list(size = 24),
showarrow = FALSE)) %>%
config(displayModeBar = F)
})
}

# Run the application
shinyApp(ui = ui, server = server)

最佳答案

要将您的绘图嵌入到 div 中,您应该使用 div()而不是 h2() ,并且要居中,只需指定其 align="center"范围:

div(plotlyOutput("plot1", height = "100%"), align = "center")

关于r - R Shiny 的中心 plotly 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42913364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com