我在Shiny中的ggplot有问题。我是Shiny的新手,因此代码中可能存在一些菜鸟错误。但是我收到以下警告:
Listening on http://127.0.0.1:4278`
Warning: Removed 93 rows containing non-finite values (stat_smooth).
Warning: Removed 93 rows containing missing values (geom_point).
Warning: Removed 1 rows containing missing values (geom_text).
R代码:
library(shiny)
library(ggplot2)
ggplot_df <- data.frame("start_ts"=c(1555279200,1555280100,1555281000,1555281900,1555282800),
"V1"=c(6.857970e-04,7.144347e-05,1.398045e-06,2.997632e-05,2.035446e-06),
"sum"=c(20,21,22,15,23))
# Small test data set with 5 observations... 93 in original one
# Define UI for application
ui <- fluidPage(sliderInput("time", "Time:",
min = as.POSIXct("00:00",format="%H:%M", tz=""),
max = as.POSIXct("24:00",format="%H:%M", tz=""),
value = c(
as.POSIXct("00:00",format="%H:%M")
), timeFormat = "%H:%M", step=60*15, timezone = "",
animate=
animationOptions(interval=300, loop=TRUE)),
plotOutput("plot")
)
# Define server logic required
server <- function(input, output) {
output$plot<-renderPlot({
ggplot_df$start_ts <-as.POSIXct(ggplot_df$start_ts, format="%H:%M", tz="",origin="1970-01-01")
ggplot_df<-ggplot_df[ggplot_df$start_ts==input$time,]
ggplot(ggplot_df,aes(x=sum,y=V1))+geom_point() +
theme_bw() +
geom_smooth(method = "lm", se = FALSE) +
ylim(0,3) +
xlim(0,max(ggplot_df$sum)) +
annotate('text', max(ggplot_df$sum)-10,3,
label = paste("~R^{2}==",round(cor(ggplot_df$sum, ggplot_df$V1), digits=2)),parse = TRUE,size=4)
})
}
# Run the application
shinyApp(ui = ui, server = server)
请注意,即使我定义了时区,也发生了完全相同的事情。
ggplot_df
是具有93行的数据帧。我做错了什么?我收到的图是空的,没有点,等等,如下所示:
答案 0 :(得分:0)
问题在于POSIXct列是日期时间,但是滑块输入仅是时间。这里重要的日期是一天中唯一有趣的时间吗?下面的代码作了一些图,尽管我不能说出所需的最终结果是什么,所以可能不太正确
ui <- fluidPage(sliderInput("time", "Time:",
min = as.POSIXct("2019-04-14 00:00",format="%Y-%m-%d %H:%M", tz=""),
max = as.POSIXct("2019-04-15 24:00",format="%Y-%m-%d %H:%M", tz=""),
value = c(
as.POSIXct("2019-04-14 00:00")
), timeFormat = "%Y-%m-%d %H:%M", step=60*15, timezone = "",
animate=
animationOptions(interval=300, loop=TRUE)),
plotOutput("plot")
)
# Define server logic required
server <- function(input, output) {
output$plot<-renderPlot({
ggplot_df$start_ts <-as.POSIXct(ggplot_df$start_ts, tz="",origin="1970-01-01")
ggplot_df<-ggplot_df[ggplot_df$start_ts==input$time,]
ggplot(ggplot_df,aes(x=sum,y=V1))+geom_point() +
theme_bw() +
geom_smooth(method = "lm", se = FALSE) +
ylim(0,3) +
xlim(0,max(ggplot_df$sum)) +
annotate('text', max(ggplot_df$sum)-10,3,
label = paste("~R^{2}==",round(cor(ggplot_df$sum, ggplot_df$V1), digits=2)),parse = TRUE,size=4)
})
}