RStudio闪亮的情节的比例和大小

Hen*_*ndy 53 size r rstudio shiny

相关,但一般只讨论分配的绘图空间,而不是如何直接设置绘图图像大小然后缩放它以填充所需的空间


我正在创建一个闪亮的Web应用程序,并希望设置绘图比例的大小.我的意思是我正在寻找一种方法来为我的绘图设置有限的高度/宽度,然后将设置大小的图像缩放到该mainPanel( plotOutput ())区域.

以此作为例子/类似情况shiny.

x <- 1:10
y <- x^2
png("~/Desktop/small.png", width = 600, height = 400)
plot(x, y)
dev.off()

png("~/Desktop/big.png", width = 1200, height = 800)
plot(x, y)
dev.off()
Run Code Online (Sandbox Code Playgroud)

我无法将图像上传到SO并设置大小,因此我将使用以下html包含每个的浏览器屏幕截图:

<img src="file:///home/jwhendy/Desktop/file.png" width = "800px" />
Run Code Online (Sandbox Code Playgroud)

这是我1600 x 900 px笔记本电脑上的全宽屏幕截图.

小图片

更大的图片

我想控制图像本身的大小,因为我ggplot2在使用类似的选项时会发现这些图例colour = var并且size = var非常小.注意读取大图片的轴标签的难度.我意识到我可能会遇到由于像素有限而无法很好地缩放图像尺寸的情况,但我认为在遇到这种情况之前我至少有一些旅行空间.

有什么建议?到目前为止我试过玩下面的内容,但没有运气:

ui.R

shinyUI(pageWithSidebar(

headerPanel("Title"),

  sidebarPanel(),

  mainPanel(

     plotOutput(outputId = "main_plot", width = "100%"))

  ))
Run Code Online (Sandbox Code Playgroud)

server.R

shinyServer(function(input, output) {

  x <- 1:10
  y <- x^2

  output$main_plot <- renderPlot({

    plot(x, y) }, height = 400, width = 600 )
} )
Run Code Online (Sandbox Code Playgroud)

似乎server.R覆盖指定的高度/宽度选项覆盖我在plotOutput部分中设置的任何内容ui.R.

有没有办法保持绘图图像的尺寸更小,以保持可读性,同时仍然填充所需的mainPanel区域?

Ram*_*han 33

不确定这是否能满足你的需求,但这对我有用.

指定的选项Server.R确实生效.(我只是绘制了两张不同大小的图表.)我还采用了@ Manetheran的建议,并将cex和cex.axis作为参数.他们似乎在工作.

以下是完整应用程序的代码,以及一个屏幕截图.

###UI.R
shinyUI(pageWithSidebar(


  headerPanel("Title"),

  sidebarPanel(
    sliderInput(inputId = "opt.cex",
            label = "Point Size (cex)",                            
                min = 0, max = 2, step = 0.25, value = 1),
  sliderInput(inputId = "opt.cexaxis",
              label = "Axis Text Size (cex.axis)",                            
              min = 0, max = 2, step = 0.25, value = 1) 
),

  mainPanel(
    plotOutput(outputId = "main_plot",  width = "100%"),
    plotOutput(outputId = "main_plot2", width = "100%")
  )
))


###Server.R
shinyServer(function(input, output) {


  x <- 1:10
  y <- x^2

  output$main_plot <- renderPlot({    
    plot(x, y)}, height = 200, width = 300)


  output$main_plot2 <- renderPlot({
    plot(x, y, cex=input$opt.cex, cex.lab=input$opt.cexaxis) }, height = 400, width = 600 )
} )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

更新重新.宽度= 100%选项UI.R

是的,就我而言,它确实有所作为.在下面的两行中,new_main_plot和new_main_plot2是相同的,但它们以不同的大小呈现.所以width选项确实生效了.

 mainPanel(
    plotOutput(outputId = "new_main_plot",  width = "100%"),
    plotOutput(outputId = "new_main_plot2", width = "25%")
  )
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • 在你看来,`width = 100%` 选项在`ui.R` 中有什么作用吗?看起来它只是在 `renderPlot()` 中指定的大小,无论如何?不管怎样,我认为这会奏效。我使用了 `base` 中的 `plot()` 命令,但实际上使用的是 `ggplot2`(没有预料到特定于 `plot` 的解决方案。我认为 [THIS](http://stackoverflow.com/questions/ 4528161/cex-equivalent-in-ggplot2) 将允许我做同样的事情。多一点打字......但我认为它应该有效。谢谢! (2认同)

Hen*_*ndy 16

@Ram的答案对于base绘图工具非常有用.

只是为了扩展其他路人,用ggplot做到这一点:

p <- ggplot(data, aes(x = x, y = y)) + geom_point()
p <- p + theme(axis.text = element_text(size = 20)) # tweak size = n until content
print(p)
Run Code Online (Sandbox Code Playgroud)

至少会有什么影响,取决于你想要什么.有关基本概念,请参阅此问题,但请参阅有关设置文本选项的当前ggplot2 0.9.3文档 - 您可以指定轴文本,图例文本,facet_grid条带文本等.

自上一个SO问题以来,规范已经从opts([option] = theme_text(size = n))变为theme([option] = element_text(size = n)).它可能会再次改变...所以只需找到当前的测试/主题/选项页面ggplot2.


我打了一下周围还基于@拉姆的随访,并认为我了解ui.Rserver.R互动设置不同的选项,并看Inspect element在Chromium.

ui.R设置可填充的可用绘图区域,server.R定义绘图大小.

  • ui.R:width = 100%server.R,:没有选项设置

在此输入图像描述

  • ui.R:width = 100%server.R,:width = 800,height = 600

在此输入图像描述

  • ui.R:width = 50%server.R,:width = 800,height = 600

在此输入图像描述

  • ui.R:width = 200%server.R,:width = 800,height = 600

在此输入图像描述

最后一个是我需要看到的才能最终回答这个问题.如果您复制设置,您将看到绘图区域<div>确实是屏幕宽度的200%,并在浏览器中创建一个水平滚动条...但是图像仍然是固定大小.

但是,您可以生成大于绘图区域的图像(以像素为单位),然后通过ui.R并指定width = n %选项将其缩小.

所以...我不能用像素创建一个更小的图像(相对于整个图形创建更大的文本大小)然后使其更大以获得更大的文本.实际上,确实需要创建绘图区域大小(或更大)的图形,并通过绘图(或ggplot)命令本身将文本缩放到满意.

人们也不能指定(至少目前)宽度的%server.R,大概是因为它将高度/宽度选项传递给png()命令以生成图像.尝试这样做会出错(但值得一试).

我不同意指定文本大小选项比我想要的更容易,但至少我有一些有用的东西.我一直在为LaTeX文档做"作弊"的方式pdf("file.pdf", width = 8, height = 6),创建我的情节,然后在将它包含在.tex文件中时将其缩放.在我看来,比theme(axis.text = element_text(size = x))每个情节都更为简单.