R 图例更改图形中ggplot图形的大小

R 图例更改图形中ggplot图形的大小,r,ggplot2,shiny,size,legend,R,Ggplot2,Shiny,Size,Legend,我在Shiny中有一个ggplot,它使用geom_point绘制一些数据。我有它的设置,这样当一个复选框被选中,一个美学添加到两个单独的变量颜色的数据。这也创造了一个传奇。我的问题是,当这个图例出现时,它“占用”了绘图的空间,绘图变得稍微小一些。是否有一种方法可以固定绘图的大小,以便在不改变绘图大小的情况下显示图例 ui <- fluidPage( titlePanel("Transfers Analysis App"), sidebarLayout( sidebarP

我在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)

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

使用此post(),您可以:

#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)
#提取图例的函数

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

使用此post(),您可以:

#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)
#提取图例的函数

g_Legend既然你要多走一步,你能确保你的代码能够运行吗?(语法和数据)这里缺少数据,加上几个括号…很抱歉我的代码质量(我对stack和R都是新手),我不确定我是否能够提供数据,因为这是我正在处理的公司项目。我意识到这并没有多大帮助,但我想也许有人可以告诉我哪里出了问题,或者仅仅通过查看我的代码就可以提出一些函数/技术?谢谢,既然你要多做一点,你能确保你的代码能够运行吗?(语法和数据)这里缺少数据,加上几个括号…很抱歉我的代码质量(我对stack和R都是新手),我不确定我是否能够提供数据,因为这是我正在处理的公司项目。我意识到这并没有多大帮助,但我想也许有人可以告诉我哪里出了问题,或者仅仅通过查看我的代码就可以提出一些函数/技术?谢谢你,它起作用了!你知道如何把它移近图形吗?不用担心,我已经弄明白了(我在UI部分的第二个绘图输出中添加了'height=“100px')太好了,它正在工作!知道如何将其移近图形吗?不用担心,我已经解决了(我在UI部分的第二个绘图输出中添加了“height=“100px”)