r-如何将日期作为x轴的平滑线绘制在一个r短划线中



在尝试实现此功能时遇到一些问题。我几乎有一个数据集,它有两个相关的特性,一个值和一个日期(YYYY-MM-DD(。每个日期都有很多值,我想通过每个日期值的散点图的平均值(或中值(来拟合多项式LOB。日期存储为日期对象,值存储为浮点值。我也在RShiny的仪表板上做这件事。

我试过了,但没有成功。

output$plot<- renderPlotly({
ggplot(df(), aes(x = date, y = value,
text = paste('Headline: ', headline, 'n Author: ', author, 'n value: ', value))) + 
geom_point(na.rm = TRUE) + 
ggtitle('Overall Value') +
ylim(-1, 1) +   
stat_smooth()
})

我得到的只是一些输出,但没有在图形上进行线渲染。

`geom_smooth()` using method = 'loess' and formula 'y ~ x'

我试过其他几种方法,但没有成功。有没有更好的方法来解决这个问题?

不知何故,stat_smooth不喜欢aes中的text,所以我不得不对映射进行一些处理。请注意,使用ylim而不是coord_cartesian可能会更改stat_smooth的计算。

library(shiny)
library(ggplot2)
library(plotly)
headline = "test"
author = "starja"
value = "none"
set.seed(8)
test_data <- data.frame(date = rep(seq(as.Date("2000/1/1"), as.Date("2003/1/1"), by = "quarter"), each = 3),
value = rnorm(39))
ui <- fluidPage(
plotlyOutput("test_plot")
)
server <- function(input, output, session) {
output$test_plot<- renderPlotly({
p <- ggplot() + 
geom_point(data = test_data, mapping = aes(x = date, y = value,
text = paste('Headline: ', headline, 'n Author: ', author, 'n value: ', value)), na.rm = TRUE) + 
ggtitle('Overall Value') +
ylim(c(-1, 1)) +
stat_smooth(aes(x = date, y = value), data = test_data)

ggplotly(p)

})
}
shinyApp(ui, server)

相关内容

最新更新