gpt4 book ai didi

r - 如何在 R Shiny 中增加 ggplot2 图的绝对大小?

转载 作者:行者123 更新时间:2023-12-04 14:59:56 27 4
gpt4 key购买 nike

我有一个 R Shiny 应用程序,它接收用户 csv 文件并绘制 csv 文件的图形。

我的 R Shiny 程序使用的是 tabsetPanel,这比我想要的更能缩小图形。

我可以添加 ggplot() 中的属性来增加图形的大小吗?

我还注意到,当我尝试在一个选项卡中绘制多个图形时,由于 UI 的高度有限,我只能转到 2 行图形。我怎样才能延长这个?

现在我只有一个选项卡,但我计划稍后添加更多选项卡。

这是我的代码:

用户界面

dataset <- list('Upload a file'=c(1))

shinyUI(pageWithSidebar(

headerPanel(''),

sidebarPanel(

wellPanel(
fileInput('file', 'Data file'),
radioButtons('format', 'Format', c('CSV', 'TSV'))
),

wellPanel(
selectInput('xMulti', 'X', names(dataset)),
selectInput('yMulti', 'Y', names(dataset), multiple=T)

)

wellPanel(
checkboxInput('normalize', 'Normalize y axes', value=TRUE)
),


wellPanel(
sliderInput("cols",
"Plots per row",
min = 1,
max = 4,
value = 2
)
)
)

mainPanel(
tabsetPanel(
tabPanel("Multiplot", plotOutput('plotMulti'), value="multi"),
id="tsp" #id of tab
)


)
))

服务器
library(reshape2)
library(googleVis)
library(ggplot2)

#Increase max upload size
options(shiny.maxRequestSize=-1)

shinyServer(function(input, output, session) {


data <- reactive({

if (is.null(input$file))
return(NULL)
else if (identical(input$format, 'CSV'))
return(read.csv(input$file$datapath))
else
return(read.delim(input$file$datapath))
})

observe({
df <- data()
str(names(df))


if (!is.null(df)) {

updateSelectInput(session, 'xMulti', choices = names(df))
updateSelectInput(session, 'yMulti', choices = names(df))


}
})



# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
require(grid)

# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)

numPlots = length(plots)

# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}

if (numPlots==1) {
print(plots[[1]])

} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}

output$plotMulti <- renderPlot({
if (is.null(data()))
return(NULL)


plots <- list() # new empty list
names <- input$yMulti

maxVal <- 0

for (i in 1:length(input$yMulti)) {
maxVal <- max(maxVal, max(data()[names[i]]))
}

for (i in 1:length(input$yMulti)) {
temp <- input$xMulti


p <- ggplot(data(), aes_string(x=temp, y=names[i]))
p <- p + opts(axis.text.x=theme_text(angle=45, hjust=1, vjust=1))
p <- p + labs(title=paste("",input$xMulti," VS ",input$yMulti,""))
h <- ggplot(data(), aes_string(x=temp))
h <- h + opts(axis.text.x=theme_text(angle=45, hjust=1, vjust=1))
h <- h + labs(title=paste("",input$xMulti," VS ",input$yMulti,""))

if (input$normalize) {
p <- p + scale_y_continuous(limits = c(0, maxVal))
h <- h + scale_y_continuous(limits = c(0, maxVal))
}

if (input$type == "Scatter") {
p <- p + geom_point(size = 3)
plots[[i]] <- p
} else if (input$type == "Line"){
p <- p + geom_line(aes(group=1)) + geom_point()
plots[[i]] <- p
} else if (input$type == "Bar") {
p <- p + geom_bar()
plots[[i]] <- p
} else if (input$type == "Histogram") {
h <- h + geom_histogram(aes(fill = ..count..))
h <- h + scale_fill_gradient("Count", low = "green", high = "red")
plots[[i]] <- h
}

}


multiplot(plotlist = plots, cols=input$cols)


})
})

最佳答案

renderPlot ,可以添加参数heightwidth以像素为单位。

(已添加,并于 2017 年 4 月更正)

要避免滚动条,请使用 renderPlot({whatever}, height="auto")

关于r - 如何在 R Shiny 中增加 ggplot2 图的绝对大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18073490/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com