RStudio 中绘图的比例和大小闪亮 [英] Scale and size of plot in RStudio shiny

查看:140
本文介绍了RStudio 中绘图的比例和大小闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<小时>

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

以此为例子/类似情况在 shiny 之外.

x <- 1:10y <- x^2png("~/Desktop/small.png", 宽度 = 600, 高度 = 400)绘图(x,y)dev.off()png("~/Desktop/big.png", width = 1200, height = 800)绘图(x,y)dev.off()

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

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

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

我想控制图像本身的大小,因为我在使用 colour = varsize = var 等选项时发现了 ggplot2 图例 非常小.请注意阅读大图的轴标签的难度.我意识到我可能会遇到图像大小因像素有限而无法很好缩放的情况,但我认为在遇到这种情况之前,我至少有一些空间可以移动.>

有什么建议吗?到目前为止,我已经尝试使用以下方法,但没有运气:

ui.R

shinyUI(pageWithSidebar(标题面板(标题"),侧边栏面板(),主面板(plotOutput(outputId = "main_plot", width = "100%"))))

server.R

shinyServer(function(input, output) {x <- 1:10y <- x^2输出$main_plot <- renderPlot({绘图(x,y)},高度= 400,宽度= 600)})

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

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

解决方案

不确定这是否完全满足您的需求,但这是对我有用的方法.

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

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

###UI.R闪亮的UI(页面带侧边栏(headerPanel(标题"),侧边栏面板(滑块输入(inputId =opt.cex",label = "点大小 (cex)",最小值 = 0,最大值 = 2,步长 = 0.25,值 = 1),滑块输入(inputId =opt.cexaxis",label = "轴文本大小 (cex.axis)",最小值 = 0,最大值 = 2,步长 = 0.25,值 = 1)),主面板(plotOutput(outputId = "main_plot", width = "100%"),plotOutput(outputId = "main_plot2", width = "100%"))))###Server.R闪亮服务器(功能(输入,输出){x <- 1:10y <- x^2输出$main_plot <- renderPlot({绘图(x,y)},高度= 200,宽度= 300)输出$main_plot2 <- renderPlot({绘图(x,y,cex=input$opt.cex,cex.lab=input$opt.cexaxis)},高度= 400,宽度= 600)})

更新.UI.R

中的 Width=100% 选项

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

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

希望有所帮助.

Related, but only talks about the allocated plot space in general, not how to directly set the plot image size and then scale it to fill the desired space


I'm creating a shiny web app and would like to set the size of the plot and scale. What I mean by that is I'm looking for a way to set a finite height/width for my plot, and then scale that set sized image to the mainPanel( plotOutput ()) area.

Take this as an example/analogous situation outside of 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()

I can't upload images to SO and set a size, so I'll include a browser screenshot of each using the following html:

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

This is a full width screenshot on my 1600 x 900 px laptop.

Small

Big

I'd like to control the size of the image itself, as I find the ggplot2 legends when using options like colour = var and size = var to be pretty small. Note the difficulty of reading axis labels for the bigger picture as well. I realize I may get into a situation where the size of the image doesn't scale well due to limited pixels, but I think I at least have some room to travel before I run into that.

Any suggestions? I've tried playing with the following so far, but without luck:

ui.R

shinyUI(pageWithSidebar(

headerPanel("Title"),

  sidebarPanel(),

  mainPanel(

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

  ))

server.R

shinyServer(function(input, output) {

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

  output$main_plot <- renderPlot({

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

It seems that the height/width options specified in server.R override whatever I have set in the plotOutput section of ui.R.

Is there a way to keep the size of the plot image smaller to maintain readability while still filling the desired mainPanel area?

解决方案

Not sure if this gives you fully what you desire, but here's what worked for me.

The options specified in Server.R did take effect. (I just plotted two graphs of different sizes each.) I also took @Manetheran's suggestion and made cex and cex.axis into parameters. They seem to be working.

Below is the code for the full app, plus one screen shot.

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

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

Yes, in my case it definitely makes a difference. In the two lines below, new_main_plot and new_main_plot2 are identical, but they were rendered with different sizes. So the width option does take effect.

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

Hope that helps.

这篇关于RStudio 中绘图的比例和大小闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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