簡體   English   中英

圖例更改了閃亮的ggplot圖形的大小

[英]Legend changing the size of ggplot figure in shiny

我在Shiny中有一個ggplot,它使用geom_point繪制一些數據。 我進行了設置,以便在選中復選框時,添加了一種美感,可以將數據着色為兩個單獨的變量。 這也創建了一個圖例。 我的問題是,當出現此圖例時,它從圖上“占用”了空間,圖變得略小。 有沒有一種方法可以固定地塊的大小,以使圖例出現而無需更改地塊的大小?

ui <- fluidPage(
  titlePanel("Transfers Analysis App"),

  sidebarLayout(
    sidebarPanel(
      checkboxInput("Outage", "Show Outages", FALSE)
    ),
    mainPanel(
      plotOutput("plot1", height = "600px", width = "100%", hover = hoverOpts(id = "plot_hover")),
      verbatimTextOutput("hover_info")
    )
  )
)

server <- function(input, output) {

  output$plot1 <- renderPlot({
    Outage <- input$Outage

    g <- ggplot(data, aes(Date, NUMBER_OF_TRANSFERS)) + geom_point() 

    if (Outage == TRUE) 
      g <- g + geom_point(aes(color = Outage))  + scale_colour_manual(breaks = c("Outage", "No Outage", "Day After an Outage", "Both"), name= "Legend", values=c( "black", "red", "blue")) + theme(legend.position="bottom")

    plot(g)
  })
}

shinyApp(ui, server)

注意:我的實際代碼中有許多功能比為簡單起見而刪除的功能要多。

也許有人有更好的主意,但這是一個建議。 您只能繪制同一圖形的圖例。 您沒有提供數據集,所以我以虹膜數據集為例。 如果單擊中斷,它將在第一個圖形的底部顯示一個圖例。 如果取消單擊,將生成您看不到的空白圖。 如您所見,圖例不會更改第一個圖形的大小。

使用本文( 如何僅繪制ggplot2中的圖例? ),您可以:

#function to extract the legend
g_legend<-function(a.gplot){ 
  tmp <- ggplot_gtable(ggplot_build(a.gplot)) 
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
  legend <- tmp$grobs[[leg]] 
  return(legend)} 

ui <- fluidPage(
  titlePanel("Transfers Analysis App"),

  sidebarLayout(
    sidebarPanel(
      checkboxInput("Outage", "Show Outages", FALSE)
    ),
    mainPanel(
      plotOutput("plot1", height = "600px", width = "100%", hover = hoverOpts(id = "plot_hover")),
      plotOutput("plot2"),
      verbatimTextOutput("hover_info")
    )
  )
)

server <- function(input, output) {

  output$plot1 <- renderPlot({
    Outage <- input$Outage

    g <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() 

    if (Outage == TRUE) 
      g <- g + geom_point(aes(color = Species))  + scale_colour_manual(breaks = c("setosa", "virginica", "versicolor"), values=c( "black", "red", "blue")) + 
      theme(legend.position="none") 
    plot(g)

  })


  output$plot2 <- renderPlot({

    Outage <- input$Outage

    if (Outage == TRUE) {
      g <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() 

      g <- g + geom_point(aes(color = Species))  + scale_colour_manual(breaks = c("setosa", "virginica", "versicolor"), name= "Legend", values=c( "black", "red", "blue")) + 
        theme(legend.position="bottom") +
        theme(legend.text=element_text(size=15)) # you can change the size of the legend

      legend <- g_legend(g) 
      grid.draw(legend) 
    } else {
      g <- ggplot()  + theme_bw(base_size=0) +
        theme(axis.line = element_line(colour = "black"),
              panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.border = element_blank(),
              panel.background = element_blank()) 

      plot(g)
    }

  })  
}


shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM