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非常小.注意读取大图片的轴标签的难度.我意识到我可能会遇到由于像素有限而无法很好地缩放图像尺寸的情况,但我认为在遇到这种情况之前我至少有一些旅行空间.
有什么建议?到目前为止我试过玩下面的内容,但没有运气:
shinyUI(pageWithSidebar(
headerPanel("Title"),
sidebarPanel(),
mainPanel(
plotOutput(outputId = "main_plot", width = "100%"))
))
Run Code Online (Sandbox Code Playgroud)
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)

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)
希望有所帮助.
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.R和server.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))每个情节都更为简单.