如何在一行中放置两个图形图?

时间:2017-08-14 18:55:52

标签: r plotly shiny

我的Shiny应用程序中有多个饼图,我似乎无法移动它们以便它们适合同一行。我可以通过更改.css文件中的位置或使用absolutePanel()来移动它们,但是任何一个选项都会在底部留下一大块空白空间。我无法从我的应用程序发布代码,所以我做了一个较小的例子,其中我尝试使用column()函数无效。任何帮助,将不胜感激。在我的实际应用程序中,我有大约5个图表,所以白色空间的数量非常大,我希望解决它。这可能是一个简单的答案,但请耐心地告诉我们我是新手...

library(shiny)
library(plotly)


ui = fluidPage(
  plotlyOutput("graph1",width="40%",height="350px"),
  column(width=12,offset=4,plotlyOutput("graph2",width="40%",height="350px"))
)
server = function(input, output,session) {



  output$graph1<-renderPlotly({

    USPersonalExpenditure <- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
    data <- USPersonalExpenditure[,c('Categorie', 'X1960')]

    p <- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie') %>%
      layout(title = 'Graph 1',
             xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
             yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

  })
  output$graph2<-renderPlotly({
    USPersonalExpenditure <- data.frame("Categorie" = rownames(USPersonalExpenditure), USPersonalExpenditure)
    data <- USPersonalExpenditure[, c('Categorie', 'X1960')]

    colors <- c('rgb(211,94,96)', 'rgb(128,133,133)', 'rgb(144,103,167)', 'rgb(171,104,87)', 'rgb(114,147,203)')

    p <- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie',
                 textposition = 'inside',
                 textinfo = 'label+percent',
                 insidetextfont = list(color = '#FFFFFF'),
                 hoverinfo = 'text',
                 text = ~paste('$', X1960, ' billions'),
                 marker = list(colors = colors,
                               line = list(color = '#FFFFFF', width = 1)),
                 #The 'pull' attribute can also be used to create space between the sectors
                 showlegend = FALSE) %>%
      layout(title = 'Graph 2',
             xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
             yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

  })
}

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

1 个答案:

答案 0 :(得分:1)

您可以尝试使用fluidRow并按列进行细分,如下所示。列空间为12,因此在一个column(6,..)内进行两次fluidRow次调用会将您分成两部分。下面的示例分为4.我不清楚底部的#34;白色空间&#34;问题是你描述的,所以这可能不是你之后的修复。

ui = fluidPage(
fluidRow(
    column(3,
           plotlyOutput("graph1")
           ),
    column(3,
           plotlyOutput("graph2")
           ),
    column(3,
           plotlyOutput("graph3")
           ),
    column(3,
           plotlyOutput("graph4")
           )
)
)