- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有时间序列数据,每个时间都有一个观察结果。我通过 R 运行它 splinefun
为观测值创建样条函数。我想在 ggplot2
中绘制这个样条函数使用 stat_function
.我希望我的绘图将日期/时间作为 X 轴,而不是按行索引的 X 轴。可以说我已经尝试了很多东西,从编辑 splinefun
的结果开始。 (尝试使其与 as.POSIXct 一起使用),在 ggplot 命令中抛出不同的美学。
如何获取 X 轴上的日期/时间?
这是我目前的情节:
这是一个reprex:
library(ggplot2)
DateTime <- seq.POSIXt(from = as.POSIXct('2020-01-10'),
to = as.POSIXct('2020-01-12'),
by = '1 hour')
set.seed(1)
y <- runif(length(DateTime), min = 0.5) * cos(as.numeric(DateTime))
df <- data.frame(DateTime = DateTime,
x = seq(1:length(DateTime)),
y = y)
myspline <- splinefun(df$x, df$y)
ggplot(mapping = aes(x=1:nrow(df))) +
stat_function(fun = myspline, size = 1, args = list(deriv = 0))
最佳答案
splinefun
函数似乎对我来说用 POSIXct 值工作得很好
set.seed(1)
df <- transform(data.frame(DateTime = seq.POSIXt(from = as.POSIXct('2020-01-10'),
to = as.POSIXct('2020-01-12'),
by = '1 hour')),
y=runif(length(DateTime), min = 0.5) * cos(as.numeric(DateTime)))
library(ggplot2)
myspline <- splinefun(df$DateTime, df$y)
ggplot(df, mapping = aes(x=DateTime)) +
stat_function(fun = myspline, size = 1, args = list(deriv = 0))
关于R:使用时间序列数据与 'splinefun' 和 ggplot2 'stat_function',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61348794/
我有以下代码: library("ggplot2") f = 2) {-1 + x + 0.3} else {0} } graph f(1) [1] 0 > > f(3) [1] 2.
我想绘制一系列独立伯努利分布随机变量 y 的对数似然函数,参数 p 是某个特征 x 的函数(逻辑函数)。该逻辑函数还有一个参数 b。这是我要估计的参数。所以我想将对数似然绘制为 b 的函数。我想在 R
我正在尝试绘制对数标度的点直方图(用点而不是条显示值的直方图)。结果应如下所示: MWE: 让我们模拟一些数据: set.seed(123) d <- data.frame(x = rnorm(100
我有以下代码: library("ggplot2") data =1.9,-2+x+0.1,0) } m <- function(x) { f1(x) + f2(x) } graph <- g
我有一个图表,我想在图表上叠加一个函数形式。看起来很简单,但我错过了一些东西。这是我使用的示例数据和插图 stat_function尝试添加 f1到图表: library(ggplot2) sampl
我有一些右偏数据,我想使用 ggplot 直观地比较分布拟合与常规比例和对数比例的数据。但是,当我使用 scale_x_continuous() 或 scale_x_log10() 转换分布曲线时,转
我正在研究一个数据集,对一个参数加类进行多次观察。数据是这样的: test<-data.frame(t = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.33, 0.33, 0.
在 data.frame 中,我希望能够将 ggplot2::geom_violin() 的密度估计值与使用 stat_function() 计算的密度估计值以及每个因子的密度估计值进行比较。 在此设
我看到很多关于如何自定义图例的问题,但我什至无法自定义图例。我想有一个图例来解释黑线是二次方,绿线是三次方。 library(ggplot2) myfun1 <- function(x) x^2 my
所以我的示例数据是: x <- runif(1000, min = 0, max = 5) y <- (2 / pi) * atan(x) z <- floor(x) df <- data.frame
我正在寻找一种方法来在单个 ggplot 中自动绘制任意数量的 stat_function 对象,每个对象都有一组不同的参数,并为它们着色。 最初我想有一个大数据表,其中包含来自每个分布的大量样本,每
这是我现在拥有的代码: library(ggplot2) normal 2] 2] <- NA return(y) } ggplot(data.frame(x=c(-3,3)), aes(x=x,
我有一个关于如何将 fitdistr 计算的 args 传递给 stat_function 的后续问题(有关上下文,请参阅 here)。 我的数据框是这样的(完整数据集见下文): > str(smal
我有时间序列数据,每个时间都有一个观察结果。我通过 R 运行它 splinefun为观测值创建样条函数。我想在 ggplot2 中绘制这个样条函数使用 stat_function .我希望我的绘图将日
当我尝试使用 ggplot 绘制标准正态分布(使用 stat_function 很容易)并为不同五分位数的曲线下区域着色时,出现了这个问题 - 在我创建了一个数据框后,我能够使用 geom_line
我想使用 ggplot2 绘制离散概率分布(如泊松分布)。 我能够在不使用 ggplot2 的情况下绘制它。 plot( dpois( x=0:20, lambda=1 ), type="b") 而且
我正在使用 scale_colour_manual指定图例中可能的键。但是,如果我使用 stat_function绘制自定义函数,图例丢失。 任何想法为什么会发生这种情况? library(ggplo
我正在尝试使用以下代码创建一个图形来显示不同的饱和度及其对演讲采样动态的影响: max <- 2 decay <- function(x, k, C) { C * (1 - exp(-k*x)
我在尝试了解 R 的 ggplot2 中的 stat_function 时遇到了严重的问题。我从这个简单的例子开始: ggplot(data.frame(x = c(1, 1e4)), aes(x))
我想添加一个 stat_function图层到一个绘图,其美学映射到某个变量的状态,该变量标识一组参数。我在下面的最小工作示例中手动创建了两个 stat_function 行。这通常是结果的样子。 p
我是一名优秀的程序员,十分优秀!