Shiny:由plotOutput()和/或renderPlot()添加的不需要的空间 [英] Shiny: unwanted space added by plotOutput() and/or renderPlot()

查看:298
本文介绍了Shiny:由plotOutput()和/或renderPlot()添加的不需要的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

plotOutput 或 renderPlot 似乎会在一个绘图周围添加一堆额外的空白区域。我已经添加了背景颜色的情节和布局列来说明这一点。本质上,我想完全摆脱这个空白区域,只留下蓝色区域,然后将其自身对齐到列的左侧。



我知道这可以使用 width 参数进行调整,但要使它 100%或 auto 似乎无法正常工作。

谢谢!

 库(闪亮)
库(ggplot2)

#创建示例数据
动物< - as.data (样本(1:100,1)),
rep(青蛙,样本(1:100,1))的框架(
table,Species =
c ),
rep(Dragonfly,示例(1:100,1))
)))

服务器< - 函数(输入,输出){

输出$ pieChart< - renderPlot({

#创建饼图
ggplot(动物,aes(x =,y = Freq,fill =物种)) +
geom_bar(width = 1,stat =identity)+
coord_polar(y,start = 0 )+
主题(plot.background = element_rect(fill =lightblue))
})
}

ui< - fluidPage(

fluidRow(
column(4,
style =background-color:lightgreen,
align =left,
HTML(filler< br> ),
plotOutput(pieChart)




shinyApp(ui = ui,server = server)

解决方案

我和地图有类似但不完全相同的问题。我知道地图中我想要的纵横比(对我而言,正好是1:1),就像你在饼图中所做的一样,并且我希望它尽可能多地占用响应列的宽度,从而改变高度因此。但是,我不希望它太大,所以我添加了一行逻辑来将它限制为400像素宽。



我的方法是绘制一个不可见的虚拟 ggplot 对象,然后查询客户端会话以了解其大小。然后我可以将这个大小作为显式参数传递给实际需要的图。

 #您需要包含`session`作为第三个参数
服务器< - 函数(输入,输出,会话){

#blank(绘制宽度)----
输出$ blank< - renderPlot ({
ggplot(data.frame(x = 1),aes(NA,NA))+ geom_blank()+ theme_void()
})

空白宽度< ({
#这是使它工作的魔法
bw< - session $ clientData $ output_blank_width
if(bw> 400)400 else bw
})

blankheight< - reactive({
blankwidth()/ 1.25
#您需要的任何宽高比例
})

输出$ plotofinterest < - renderPlot({
ggplot(iris [sample.int(150,50),],aes(1,fill = Species))+
geom_bar()+ coord_polar(theta =y )
},height = blankheight,width = blankwidth)
#这是必需位
}

#ui.R

ui< - fluidPage(
fluidRow(
column(4,
style =background-color:lightgreen),
plotOutput('blank', width ='100%',height = 0),
plotOutput('plotofinterest',inline = T)
),
column(8,
style =background-color :lightblue,
HTML(fill)




shinyApp(ui,server)



如果你使窗口变窄,情节会小于400px的上限,并且会占据整个列。如果将其扩大,右侧的绿色空间会变大。我没有注意到从绘制额外的图表中获取客户端信息的重大损失。即使当你拖动窗口大小来玩它时,它也会很快更新。



你需要弄清楚你想要画的纵横比。 grid 会自动将克劳斯解释之类的空白添加到不受限制的利润率,所以它只是一种眼球,直到它看起来不错。


Either plotOutput or renderPlot seems to add a bunch of extra white space around a plot. I've added background colours to the plot and the layout column to illustrate this. Essentially, I would like to be completely rid of this white space, leaving only the area coloured in blue, which should then align itself to the left of the column.

I know that this can be adjusted using the width argument, but having it a either 100% or auto doesn't seem to work properly.

Thanks!

library(shiny)
library(ggplot2)

# Create sample data
animals <- as.data.frame(
  table(Species =
          c(rep("Moose", sample(1:100, 1)),
            rep("Frog", sample(1:100, 1)),
            rep("Dragonfly", sample(1:100, 1))
          )))

server <- function(input, output) {

  output$pieChart <- renderPlot({

    # Create pie chart
    ggplot(animals, aes(x = "", y = Freq, fill = Species)) +
      geom_bar(width = 1, stat = "identity") +
      coord_polar("y", start=0) +
      theme(plot.background = element_rect(fill = "lightblue"))
  })
}

ui <- fluidPage(

  fluidRow(
    column(4, 
           style = "background-color:lightgreen",
           align = "left",
           HTML("filler<br>"),
           plotOutput("pieChart")
    )
  )
)

shinyApp(ui = ui, server = server)

解决方案

I had a similar, but not identical, issue with a map. I knew what aspect ratio I wanted in the map (exactly 1:1 for me), much like you have with your pie chart, and I wanted it to occupy as much of the width of the responsive column as it could, changing the height accordingly. However, I didn't want it to be too big, so I added a line of logic to cap it to 400 pixels wide.

My approach was to draw a dummy ggplot object that was invisible, then query the client session to learn about its size. Then I could pass that size as an explicit parameter to the actual desired plot.

# you need to include `session` as a third argument here
server <- function(input, output, session) {

  # blank (plot for width) ----
  output$blank <- renderPlot({
    ggplot(data.frame(x = 1), aes(NA, NA)) + geom_blank() + theme_void()
  })

  blankwidth <- reactive({
          # this is the magic that makes it work
    bw <- session$clientData$output_blank_width
    if (bw > 400) 400 else bw
  })

  blankheight <- reactive({
    blankwidth() / 1.25
    # whatever aspect ratio you need
  })

  output$plotofinterest <- renderPlot({
    ggplot(iris[sample.int(150,50),], aes(1, fill = Species)) + 
      geom_bar() + coord_polar(theta = "y")
  }, height = blankheight, width = blankwidth)
     # this is the necessary bit
}

# ui.R

ui <- fluidPage(
  fluidRow(
    column(4,
           style = "background-color:lightgreen",
           plotOutput('blank', width = '100%', height = 0),
           plotOutput('plotofinterest', inline = T)
    ),
    column(8,
           style = "background-color:lightblue",
           HTML("filler")
    )
  )
)

shinyApp(ui, server)

If you make the window narrower, the plot will be less than the 400px cap, and it will take up the whole column. If you make it wider, the right-side green space gets larger. I haven't noticed a big loss of performance from drawing extra plots to get this info back from the client. Even when you drag around the window size to play with it, it updates quite quickly.

You do need to figure out what aspect ratio you want it drawn at. grid will automatically add whitespace like Claus explained to the unrestricted pair of margins, so it's fair to just kind of eyeball it until it looks good to you.

这篇关于Shiny:由plotOutput()和/或renderPlot()添加的不需要的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆