传奇改变了ggplot图中闪亮的大小

问题描述 投票:0回答:1

我有一个闪亮的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)

注意:我的实际代码有很多功能,而且为了简单起见我已经删除了。

r ggplot2 shiny size legend
1个回答
2
投票

也许有人有更好的主意,但这是一个建议。您只能绘制同一图形的图例。您没有提供数据集,所以我使用iris数据集作为示例。如果单击中断,它将在第一个图形的底部生成一个图例。如果不点击,它将产生一个你看不到的空白图。如您所见,图例不会改变第一张图的大小。

使用这篇文章(How to plot just the legends in 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)
© www.soinside.com 2019 - 2024. All rights reserved.