在R中自定义绘图的标题

时间:2017-03-28 17:22:37

标签: r shiny plotly

是否可以在R?

中将标题应用于如下图所示的情节条形图

enter image description here

这是我到目前为止的代码:

p <- plot_ly(s.df, x = ~Farm, y = ~Acreage, color = ~Crop) %>%
         layout(barmode = "stack", xaxis = list(title = ''), margin = list(b = 140),
               title = "ACREAGE")

1 个答案:

答案 0 :(得分:0)

您可以使用例如更改总背景颜色layout(plot_bgcolor='green', paper_bgcolor="green")

使用开发人员工具(&#34; Inspect Element&#34;)检查Plotlys标题样式并复制其标题属性,您可以为图表创建自己的标题:

library(shiny)
ui <- fluidPage(  
        radioButtons("titleChoice", "Plot", choices=c("Nothing", "Acreage", "Percentage", "Costs")),
        conditionalPanel(condition="input.titleChoice != 'Nothing'",
                         uiOutput("plotTitle"),
                         plotlyOutput("plot"))
      )
server <- function(input, output){
   output$plotTitle <- renderUI({
       div(style='background-color:green; color:white; text-align:center; font-size:17px; font-family:"Open Sans",verdana,arial,sans-serif', 
           input$titleChoice)
   })
   output$plot <- renderPlotly({
     plot_ly(diamonds, x = ~cut, y = ~clarity, color = ~price) %>%
       layout(barmode = "stack", xaxis = list(title = ''), margin = list(b = 140))
   })
}
shinyApp(ui = ui, server = server)

请注意这里的重要部分是

div(style='background-color:green; color:white; text-align:center; font-size:17px; font-family:"Open Sans",verdana,arial,sans-serif', TITLE)`

结果如下: enter image description here