首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传说中改变了ggplot图形的大小

传说中改变了ggplot图形的大小
EN

Stack Overflow用户
提问于 2018-01-04 09:50:54
回答 1查看 1.9K关注 0票数 0

我有一个闪亮的图形,它使用geom_point绘制一些数据。我有它的设置,以便在选中复选框时,添加一个美学,将数据着色到两个独立的变量中。这也创造了一个传奇。我的问题是,当这个传说出现时,它从情节中“占用”空间,情节变得稍微小一些。有没有一种方法,我可以修正情节的大小,以使传奇出现而不改变情节大小?

代码语言:javascript
复制
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)

注意:我的实际代码中有更多的特性,这是我为简化而设计的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-04 17:41:31

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

使用此帖子(How to plot just the legends in ggplot2?),您可以:

代码语言:javascript
复制
#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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48092643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档