简体   繁体   English

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

[英]Scale and size of plot in RStudio shiny

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

Take this as an example/analogous situation outside of shiny . 以此作为例子/ 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: 我无法将图像上传到SO并设置大小,因此我将使用以下html包含每个的浏览器屏幕截图:

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

This is a full width screenshot on my 1600 x 900 px laptop. 这是我1600 x 900 px笔记本电脑上的全宽屏幕截图。

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. 我想控制图像本身的大小,因为当使用colour = varsize = var等选项时,我发现ggplot2图例非常小。 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 ui.R

shinyUI(pageWithSidebar(

headerPanel("Title"),

  sidebarPanel(),

  mainPanel(

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

  ))

server.R 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 . 似乎server.R指定的高度/宽度选项覆盖了我在server.RplotOutput部分中设置的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? 有没有办法保持绘图图像的大小更小,以保持可读性,同时仍然填充所需的主mainPanel区域?

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. Server.R指定的选项确实生效。 (I just plotted two graphs of different sizes each.) I also took @Manetheran's suggestion and made cex and cex.axis into parameters. (我只是绘制了两张不同大小的图表。)我还采用了@ Manetheran的建议,并将cex和cex.axis作为参数。 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 UI.R的宽度= 100%选项

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. 在下面的两行中,new_main_plot和new_main_plot2是相同的,但它们以不同的大小呈现。 So the width option does take effect. 因此width选项确实生效。

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

Hope that helps. 希望有所帮助。

@Ram's answer works great for the base plotting utility. @Ram的答案非常适合base绘图工具。

Just to expand for other passers-by, to do this with ggplot: 只是为了扩展其他路人,用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)

At least something to that effect, depending on what you want. 至少会有什么影响,取决于你想要什么。 See this question for the basic idea, though please refer to the current ggplot2 0.9.3 documentation about setting text options -- you can specify axis text, legend text, facet_grid strip text, etc. 有关基本概念,请参阅此问题 ,但请参阅有关设置文本选项的当前ggplot2 0.9.3文档 - 您可以指定轴文本,图例文本,facet_grid条带文本等。

Since the previous SO question, the specification has changed from opts([option] = theme_text(size = n)) to theme([option] = element_text(size = n)) . 自上一个SO问题以来,规范已从opts([option] = theme_text(size = n))更改为theme([option] = element_text(size = n)) It will probably change again... so just find the current test/theme/options page for ggplot2 . 它可能会再次改变...所以只需找到ggplot2的当前测试/主题/选项页面。


I played around a bit further based on @Ram's followup and think I understand how ui.R and server.R interact setting different options and looking at the Inspect element in Chromium. 我根据ui.R的后续内容进一步玩了一下,并认为我理解ui.Rserver.R如何交互设置不同的选项并查看Chromium中的Inspect element

ui.R sets the available plot area one can fill, and server.R defines the plot size. ui.R设置可填充的可用绘图区域,server.R定义绘图大小。

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

在此输入图像描述

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

在此输入图像描述

  • ui.R: width = 50%, 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 ui.R: width = 200%, server.R :width = 800,height = 600

在此输入图像描述

The last one was all I needed to see in order to answer the question definitively. 最后一个是我需要看到的才能最终回答这个问题。 If you replicate the settings, you will see that the plot area <div> is indeed 200% of the screen width and creates a horizontal scroll bar in the browser... however the image is still at it's fixed size. 如果您复制设置,您将看到绘图区域<div>确实是屏幕宽度的200%并在浏览器中创建水平滚动条...但是图像仍然是固定大小。

You can however, generate an image larger than the plot area (in pixels) and then scale it down via ui.R and specifying a width = n % option. 但是,您可以生成大于绘图区域的图像(以像素为单位),然后通过ui.R将其缩小并指定width = n %选项。

So... I cannot create a smaller image in pixels (which creates larger text size relative to the overall plot) and then make it bigger to have bigger text. 所以...我不能用像素创建一个更小的图像(相对于整个图形创建更大的文本大小)然后使其更大以获得更大的文本。 One does, indeed, need to create a graph the size of the plot area (or larger) and scale the text to satisfaction via the plot (or ggplot) command itself. 实际上,确实需要创建绘图区域(或更大)大小的图形,并通过绘图(或ggplot)命令本身将文本缩放到满意。

One also cannot specify, at least at present, a % for the width in server.R , presumably because it passes the height/width options to the png() command to generate the image. 至少在目前,还没有为server.R的宽度指定%,大概是因为它将高度/宽度选项传递给png()命令以生成图像。 You'll get an error trying to do that (but it was worth a shot). 尝试这样做会出错(但值得一试)。

I disagree that specifying the text size options is an easier route than what I was looking for, but at least I have something that works. 我不同意指定文本大小选项比我想要的更容易,但至少我有一些有用的东西。 I do the "cheating" way all the time for LaTeX documents with pdf("file.pdf", width = 8, height = 6) , create my plot, and then scale it however I want when including it in the .tex file. 我用pdf("file.pdf", width = 8, height = 6)为LaTeX文档做“作弊”的方式,创建我的情节,然后在将它包含在.tex文件中时将其缩放。 Much simpler, in my opinion, than futzing with theme(axis.text = element_text(size = x)) for every plot. 在我看来,比每个情节的theme(axis.text = element_text(size = x))更为简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM