如何插入放置在时间轴上的点的值?
How can I interpolate the value of points placed on a timeline?
我想创建 'x' 浓度随时间在不同位置的插值图。如果可能的话,我想水平插入这些点(即随着时间的推移),以便为每个样本获得平滑的变色水平线。
df<-data.frame(Concentration = rnorm(30), Position = rep(c(0, 1), 15), Sample = rep(c("A", "B"), 15), Date = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-30"), "days"))
df %>%
ggplot(aes(x = Date, y = Position)) +
geom_hline(yintercept = c(0,1),
size = 0.3) +
geom_tile(aes(fill = Concentration),
interpolate = T) +
xlab("Day")+
ylab("Sample")
如果有任何建议,我将不胜感激。
李
不确定我是否完全理解这个问题,但这是我认为您所问问题的一种可能解决方案。
library(tidyverse)
library(gt)
df <-
data.frame(Concentration = rnorm(30),
Sample = rep(c("A", "B"), 15),
Date = seq.Date(as.Date("2020-01-01"),
as.Date("2020-01-30"), "days"))
interpolated_df <-
df %>%
arrange(Sample) %>%
complete(Sample, Date) %>%
## interpolated is average of value right before and right after
mutate(Interpolations = (lead(Concentration) +
lag(Concentration))/2 ) %>%
mutate(Final = coalesce(Concentration, Interpolations))
## plot
interpolated_df %>%
ggplot(aes(x = Date, y = Sample)) +
geom_tile(aes(fill = Final)) +
xlab("Day")+
ylab("Sample")
由 reprex package (v2.0.1)
于 2021-10-25 创建
这正是 ggforce::geom_link
的目的。
library(tidyverse)
library(ggforce)
set.seed(42)
df <- data.frame(Concentration = rnorm(30), Position = rep(c(0, 1), 15), Sample = rep(c("A", "B"), 15), Date = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-30"), "days"))
df %>%
ggplot(aes(x = Date, y = Position)) +
geom_link2(aes(group = Position, color = Concentration), size = 10) +
labs(x = "Day", y = "Sample")
由 reprex package (v2.0.1)
于 2021-10-25 创建
我想创建 'x' 浓度随时间在不同位置的插值图。如果可能的话,我想水平插入这些点(即随着时间的推移),以便为每个样本获得平滑的变色水平线。
df<-data.frame(Concentration = rnorm(30), Position = rep(c(0, 1), 15), Sample = rep(c("A", "B"), 15), Date = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-30"), "days"))
df %>%
ggplot(aes(x = Date, y = Position)) +
geom_hline(yintercept = c(0,1),
size = 0.3) +
geom_tile(aes(fill = Concentration),
interpolate = T) +
xlab("Day")+
ylab("Sample")
如果有任何建议,我将不胜感激。 李
不确定我是否完全理解这个问题,但这是我认为您所问问题的一种可能解决方案。
library(tidyverse)
library(gt)
df <-
data.frame(Concentration = rnorm(30),
Sample = rep(c("A", "B"), 15),
Date = seq.Date(as.Date("2020-01-01"),
as.Date("2020-01-30"), "days"))
interpolated_df <-
df %>%
arrange(Sample) %>%
complete(Sample, Date) %>%
## interpolated is average of value right before and right after
mutate(Interpolations = (lead(Concentration) +
lag(Concentration))/2 ) %>%
mutate(Final = coalesce(Concentration, Interpolations))
## plot
interpolated_df %>%
ggplot(aes(x = Date, y = Sample)) +
geom_tile(aes(fill = Final)) +
xlab("Day")+
ylab("Sample")
由 reprex package (v2.0.1)
于 2021-10-25 创建这正是 ggforce::geom_link
的目的。
library(tidyverse)
library(ggforce)
set.seed(42)
df <- data.frame(Concentration = rnorm(30), Position = rep(c(0, 1), 15), Sample = rep(c("A", "B"), 15), Date = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-30"), "days"))
df %>%
ggplot(aes(x = Date, y = Position)) +
geom_link2(aes(group = Position, color = Concentration), size = 10) +
labs(x = "Day", y = "Sample")
由 reprex package (v2.0.1)
于 2021-10-25 创建