在ggplot中使用过滤器时闪亮的范围滑块错误
Posted
技术标签:
【中文标题】在ggplot中使用过滤器时闪亮的范围滑块错误【英文标题】:Shiny range slider- error when using filter in ggplot 【发布时间】:2020-05-14 22:31:26 【问题描述】:早上好,提前感谢您的帮助。我一直在尝试部署一个闪亮的应用程序,但无法让我的滑块过滤年份值,我现在有一些代码,但尝试了很多过滤不成功的方法。我在下面包含了我的代码,以及我的数据框的 str/head。
str:
$ date: POSIXct, format: "1985-01-01" "1985-02-01" "1985-03-01" "1985-04-01" ...
$ temp: num 36.4 41.7 54.9 67.4 75.2 ...
$ mean: num 32.4 38.5 56.9 71.4 77.4 ...
$ min : num 16.2 18.8 26.9 36 46.1 ...
$ max : num 49.6 52.1 62.9 68.5 78.4 ...
head:
date temp mean min max
<dttm> <dbl> <dbl> <dbl> <dbl>
1 1985-01-01 00:00:00 36.4 32.4 16.2 49.6
2 1985-02-01 00:00:00 41.7 38.5 18.8 52.1
3 1985-03-01 00:00:00 54.9 56.8 26.9 62.8
4 1985-04-01 00:00:00 67.4 71.4 36.0 68.4
5 1985-05-01 00:00:00 75.2 77.4 46.1 78.4
6 1985-06-01 00:00:00 81.3 81.2 54.9 85.0
> dput(head(AllTempNew1,20))
structure(list(date = structure(c(473385600, 476064000, 478483200,
481161600, 483753600, 486432000, 489024000, 491702400, 494380800,
496972800, 499651200, 502243200, 504921600, 507600000, 510019200,
512697600, 515289600, 517968000, 520560000, 523238400), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), temp = c(36.43, 41.68, 54.93, 67.42,
75.16, 81.28, 87.33, 83.84, 74.5, 65.75, 48.79, 39.47, 46.04,
45.95, 58.59, 65.44, 73.72, 83.16, 86.09, 84.58), mean = c(32.37,
38.54, 56.85, 71.36, 77.36, 81.22, 87.98, 82.54, 71.19, 64.7,
44.87, 36.18, 51.59, 47.08, 64.17, 67.4, 74.48, 84.98, 85.5,
84.02), min = c(16.2, 18.81, 26.94, 35.96, 46.09, 54.93, 59.4,
57.34, 50.27, 39, 28.13, 16.57, 16.2, 18.81, 26.94, 35.96, 46.09,
54.93, 59.4, 57.34), max = c(49.64, 52.14, 62.85, 68.45, 78.4,
85.03, 89.96, 88.36, 81.95, 69.89, 61.16, 48.06, 49.64, 52.14,
62.85, 68.45, 78.4, 85.03, 89.96, 88.36)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
这是我的用户界面:
shinyUI(fluidPage(
titlePanel("U.S. Monthly Temperature Highs, Lows, and Averages from 1985-2019"),
sidebarLayout(
sidebarPanel(
sliderInput("year", "Select a Range of Years to Display ", min = 1985, max = 2019,
value = c(1985, 2019), sep = ""),
checkboxInput("Min1","Min Temp", names(c("Min1"))),
checkboxInput("Max1","Max Temp", names(c("Max1"))),
checkboxInput("Mean1","Mean Temp", names(c("Mean1")))),
mainPanel(plotOutput("TempPlot")))))
这是我的服务器:
shinyServer(function(input, output)
sliderValues <- reactive(
temp <- AllTempNew1$temp
max <- input$Max1
min <- input$Min1
mean <- input$Mean1
yearrange <- AllTempNew1 %>% filter(variable %in% input$year))
output$TempPlot <- renderPlot(
TempPlot <- ggplot(AllTempNew1, aes(x= yearrange)) +
geom_line(aes(y = temp), color = "orange", linetype="solid")
TempPlot <- TempPlot + labs(title = "U.S. Monthly Temperature Highs, Lows, and Averages from 1985-2019",
x = "Year of Recorded Temperature", y = "Temperature Recorded")
TempPlot
if(input$Max1 == FALSE & input$Mean1 == FALSE & input$Min1 == FALSE)TempPlot
else if(input$Max1 == TRUE)
TempPlot + geom_line(aes(y = max), color = "red", linetype="dotdash")
else if(input$Min1 == TRUE)
TempPlot + geom_line(aes(y = min), color="blue", linetype="dotdash")
else if(input$Mean1 == TRUE)
TempPlot + geom_line(aes(y = mean), color="green", linetype="twodash")
elseTempPlot))
【问题讨论】:
嘿.. AllTempNew1 是什么?您没有在上面的示例中提供它。您可以执行 dput(head(AllTempNew1,20)) 并粘贴输出而不是显示该输出吗? 您需要使用反应值创建一个新的 data.frame,或者只是在绘图之前对其进行过滤。 对不起,AllTempNew1 是我的数据框。添加了上面的输出。我在绘制命令之前遇到了过滤器问题,我尝试了一些不同的过滤命令,但到目前为止没有一个有效 【参考方案1】:我认为你想要这样的东西,这可以通过包 lubridate 实现。
library(shiny)
library(lubridate)
AllTempNew1 <- structure(list(date = structure(c(473385600, 476064000, 478483200, 481161600, 483753600, 486432000, 489024000, 491702400, 494380800,
496972800, 499651200, 502243200, 504921600, 507600000, 510019200,512697600, 515289600, 517968000, 520560000, 523238400),
class = c("POSIXct", "POSIXt"), tzone = "UTC"),
temp = c(36.43, 41.68, 54.93, 67.42,75.16, 81.28, 87.33, 83.84, 74.5, 65.75, 48.79, 39.47, 46.04,
45.95, 58.59, 65.44, 73.72, 83.16, 86.09, 84.58),
mean = c(32.37, 38.54, 56.85, 71.36, 77.36, 81.22, 87.98, 82.54, 71.19, 64.7,
44.87, 36.18, 51.59, 47.08, 64.17, 67.4, 74.48, 84.98, 85.5, 84.02),
min = c(16.2, 18.81, 26.94, 35.96, 46.09, 54.93, 59.4, 57.34, 50.27, 39, 28.13,
16.57, 16.2, 18.81, 26.94, 35.96, 46.09, 54.93, 59.4, 57.34),
max = c(49.64, 52.14, 62.85, 68.45, 78.4, 85.03, 89.96, 88.36, 81.95, 69.89,
61.16, 48.06, 49.64, 52.14, 62.85, 68.45, 78.4, 85.03, 89.96, 88.36)),
row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))
ui <- fluidPage(
titlePanel("U.S. Monthly Temperature Highs, Lows, and Averages from 1985-2019"),
sidebarLayout(
sidebarPanel(
sliderInput("year", "Select a Range of Years to Display ", min = 1985, max = 2019,
value = c(1985, 2019), sep = ""),
checkboxGroupInput("fun", label = NULL,
choices = list("Min Temp" = 1,
"Max Temp" = 2,
"Mean Temp" = 3))
),
mainPanel(plotOutput("TempPlot"))))
server <- function(input, output)
minFun <- reactive(
if (1 %in% input$fun)
geom_line(aes(y = min), color="red", linetype="dotdash")
)
maxFun <- reactive(
if (2 %in% input$fun)
geom_line(aes(y = max), color="blue", linetype="dotdash")
)
meanFun <- reactive(
if (3 %in% input$fun)
geom_line(aes(y = mean), color="green", linetype="twodash")
)
output$TempPlot <- renderPlot(
TempPlot <- ggplot(AllTempNew1 %>% filter(between(as.numeric(year(AllTempNew1$date)), input$year[1], input$year[2])), aes(x= date)) +
geom_line(aes(y = temp), color = "orange", linetype="solid") +
labs(title = "U.S. Monthly Temperature Highs, Lows, and Averages from 1985-2019",
x = "Year of Recorded Temperature",
y = "Temperature Recorded") + minFun() + maxFun() + meanFun()
TempPlot
)
shinyApp(ui, server)
【讨论】:
谢谢!我确实打算显示多行但无法使其工作,但由于它工作到一半(一次绘制一个),我想我会尝试在滑块上投入更多精力。我会改变什么来绘制多条线?感谢您的建议!仍在尝试使用您建议的代码来使滑块正常工作。 我更改了代码以包含显示多行的可能性。我也改变了过滤方法,因为我意识到在旧代码中只用了第一年和最后一年 我为多行做了一些不同的事情并且成功了,非常感谢你,你的代码比我的效率高得多。我仍然无法让滑块工作,但感觉比今天早上更近,这是我收到的错误:“不知道如何将'x'转换为类“POSIXlt”。它目前是类 POSIXct,我尝试没有运气改变它的班级! 你的意思是当你实现我的代码时滑块不起作用还是来自其他东西的错误消息? 效果很好,非常感谢!!我非常感谢您花时间提供帮助,并回来更新和帮助,直到它起作用。再次感谢!这正是我所需要的!以上是关于在ggplot中使用过滤器时闪亮的范围滑块错误的主要内容,如果未能解决你的问题,请参考以下文章