【发布时间】:2020-12-10 23:02:50
【问题描述】:
我目前正在开发一款 Rshiny 应用程序。我必须用几个选择输入和一个复选框输入制作多个箱线图。我几乎完成了我的任务,现在我只需要完成它,以便每次更改所选物种时,比例保持不变(当然,除非输入了其他变量)。
我的代码如下所示:
library(shiny)
library(palmerpenguins)
library(dplyr)
library(ggplot2)
data <- na.omit(penguins)
colnames(data) <- c("Species",
"Island",
"Bill Length (mm)",
"Bill Depth (mm)",
"Flipper Length (mm)",
"Body Mass (g)",
"Sex",
"Year")
data.numeric <- data[, c(3:6, 8)]
data.categorical <- data[, c(1,7)]
ui <- fluidPage(
headerPanel("Penguin boxplots"),
selectInput("ycol",
"Numeric Variable",
names(data.numeric),
selected = names(data.numeric)[3]),
selectInput("xcol",
"Categorical Variable",
names(data.categorical),
selected = names(data.categorical)[2]),
checkboxInput("split",
"Split levels by Island",
value = FALSE),
selectInput("species",
"Species Type",
c("Unspecified", levels(data$Species))),
mainPanel(
plotOutput("plot1")
))
# 4. Use a select input to allow the user to view separate plotting
# panels for each species, with each plot panel on the same scale.
server <- function(input, output){
output$plot1 <- renderPlot({
par(mar = c(5, 4.1, 0, 1))
if (input$species == "Unspecified"){
if (input$split) {
ggplot(data, aes(x = data[[input$xcol]], y = data[[input$ycol]], fill = Island)) +
geom_boxplot(na.rm = TRUE) +
if (input$ycol == "Bill Length (mm)"){
coord_cartesian(ylim = c(30, 60))
} else if (input$ycol == "Bill Depth (mm)"){
coord_cartesian(ylim = c(12.5, 21.5))
} else if (input$ycol == "Flipper Length(mm)"){
coord_cartesian(ylim = c(170, 232))
} else if (input$ycol == "Body Mass (g)"){
coord_cartesian(ylim = c(2500, 6500))
} else if (input$ycol == "Year"){
coord_cartesian(ylim = c(2006, 2009))
} +
xlab(input$xcol) +
ylab(input$ycol) +
scale_x_discrete(labels = c("Female", "Male")) +
theme(text = element_text(size = 15))
} else {
ggplot(data, aes(x = data[[input$xcol]], y = data[[input$ycol]])) +
geom_boxplot(na.rm = TRUE) +
if (input$ycol == "Bill Length (mm)"){
coord_cartesian(ylim = c(30, 60))
} else if (input$ycol == "Bill Depth (mm)"){
coord_cartesian(ylim = c(12.5, 21.5))
} else if (input$ycol == "Flipper Length(mm)"){
coord_cartesian(ylim = c(170, 232))
} else if (input$ycol == "Body Mass (g)"){
coord_cartesian(ylim = c(2500, 6500))
} else if (input$ycol == "Year"){
coord_cartesian(ylim = c(2006, 2009))
} +
xlab(input$xcol) +
ylab(input$ycol) +
theme(text = element_text(size = 15))
}
} else {
data <- data %>%
filter(data$Species == input$species)
if (input$split) {
ggplot(data, aes(x = data[[input$xcol]], y = data[[input$ycol]], fill = Island)) +
geom_boxplot(na.rm = TRUE) +
xlab(input$xcol) +
ylab(input$ycol) +
scale_x_discrete(labels = c("Female", "Male")) +
theme(text = element_text(size = 15))
} else {
ggplot(data, aes(x = data[[input$xcol]], y = data[[input$ycol]])) +
geom_boxplot(na.rm = TRUE) +
xlab(input$xcol) +
ylab(input$ycol) +
theme(text = element_text(size = 15))
}
}
})
}
shinyApp(ui = ui, server = server)
当我运行包含else if (input$ycol == "Year"){ coord_cartesian(ylim = c(2006, 2009))} 的行时出现以下错误:无法将 ggproto 对象添加在一起。您是否忘记将此对象添加到 ggplot 对象中?
我不确定我在哪里出错了,或者我只是忽略了某种语法错误。
附言代码尚未完成 - 我仍然需要添加更多代码来控制物种不是“未指定”时的比例,但在弄清楚这一点之前我不会这样做。
【问题讨论】:
-
在最后一个 ggplot 语句中尝试
else而不是if else。 -
看到我之前也这么认为并尝试过,但它不起作用。我已经把它改回来了,它仍然无法正常工作。仍然说同样的错误。这是现在的代码行:else { coord_cartesian(ylim = c(2006, 2009))}
标签: r if-statement ggplot2 shiny