相关,但一般只讨论分配的打印空间,而不是如何直接设置打印图像大小并将其zoom then以填充所需的空间


我正在创建一个shiny 的web应用程序,并想设置plot and比例的大小.我的意思是,我正在寻找一种方法,为我的绘图设置有限的高度/宽度,并将大小为mainPanel( plotOutput ())的图像设置为then比例.

以这为例/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()

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

<img src="file:///home/jwhendy/Desktop/file.png" width = "800px" />

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

Small

Big

我想控制图像本身的大小,因为我发现使用colour = varsize = var这样的选项时,ggplot2个图例非常小.请注意,在大图中读取轴标签也很困难.我意识到我可能会遇到这样的情况:由于像素有限,图像的大小不能很好地zoom ,但我认为在遇到这种情况之前,我至少有some个空间可以移动.

有什么建议吗?到目前为止,我已经try 过玩以下游戏,但运气不佳:

用户界面.R

shinyUI(pageWithSidebar(

headerPanel("Title"),

  sidebarPanel(),

  mainPanel(

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

  ))

服务器R

shinyServer(function(input, output) {

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

  output$main_plot <- renderPlot({

    plot(x, y) }, height = 400, width = 600 )
} )

服务器R中指定的高度/宽度选项似乎覆盖了我在用户界面.RplotOutput部分中设置的任何选项.

有没有办法使绘图图像的大小保持较小,以保持可读性,同时仍填充所需的mainPanel区域?

推荐答案

不确定这是否能完全满足你的需求,但以下是对我有效的方法.

Server.R中指定的选项确实生效.(我刚刚画了两张大小不同的图.)我也采纳了@Maneteran的建议,提出了cex和cex.将轴转换为参数.他们似乎在工作.

下面是完整应用程序的代码,外加一个屏幕截图.

###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 )
} )

enter image description here

Update re. the Width=100% option in UI.R

是的,在我的情况下,这肯定会有所不同.在下面的两行中,new_main_plot和new_main_plot2是相同的,但它们的渲染大小不同.因此,width选项确实生效.

 mainPanel(
    plotOutput(outputId = "new_main_plot",  width = "100%"),
    plotOutput(outputId = "new_main_plot2", width = "25%")
  )

希望有帮助.

R相关问答推荐

Word中的分面图表

逐行替代引用前一行的for循环

按块将载体转换为矩阵-reshape

如何根据包含相同值的某些列获取总额

Tidyverse/Djirr为从嵌套列表中提取的列名赋值的解决方案

在(g)子中使用asserable字符

在"gt"表中添加第二个"groupname_col",而不连接列值

在R中使用download. file().奇怪的URL?

在另存为PNG之前隐藏htmlwidget绘图元素

未识别时区

将饼图插入条形图

如何在分组条形图中移动相关列?

R Read.table函数无法对制表符分隔的数据正常工作

悬崖三角洲超大型群数计算导致整数溢出

计算直线上点到参考点的总距离

如何在PDF格式的kableExtra表格中显示管道字符?

在ggplot2上从多个数据框创建复杂的自定义图形

用多边形替换地块点

在生成打印的自定义函数中,可以通过变量将线型或 colored颜色 设置为NULL吗?

是否有一个R函数可以输出在输入的字符向量中找到的相应正则表达式模式?