数据:
df = structure(list(obs_date = structure(c(17728, 17759, 17750, 17751,
17759, 17777, 17778, 17779, 17780, 17751, 17759, 17773, 17779,
17759, 17773, 17777, 17784, 17722, 17759, 17750, 17759, 17724,
17759, 17760, 17780, 17781, 17740, 17759, 17779, 17780, 17777,
17759, 17765, 17759, 17760, 17766, 17774, 17750, 17759, 17779,
17740, 17759, 17779, 17716, 17732, 17735, 17736, 17760, 17740,
17759, 17765), class = "Date", tzone = "Australia/Sydney"),
obs_value = c(0.104669, 0.109833, 0.196295, 0.2, 0.21, 0.21422, 0.21, 0.202339, 0.2,
0.24, 0.24, 0.25, 0.24, 0.209645, 0.204462, 0.204462, 0.2042,
NA, NA, 0.204, 0.224486, 0.142, 0.142, 0.144, 0.144, 0.15, NA,
0.22, 0.22, 0.22, 0.23, 0.208, 0.208, 0.213781, 0.213781, 0.23111,
0.23111, 0.2, 0.190581, 0.188411, 0.318, 0.208, 0.204, 0.31,
0.31, 0.21, 0.21, 0.21, 0.25, 0.21, 0.21),
obs_id = c("2HN", "2HN", "4GJ", "4GJ", "4GJ", "4GJ", "4GJ", "4GJ", "4GJ", "KFM",
"KFM", "KFM", "KFM", "N9S", "N9S", "N9S", "N9S", "NF7", "NF7",
"7Q6", "7Q6", "M6Q", "M6Q", "M6Q", "M6Q", "M6Q", "MW6", "YP0",
"YP0", "YP0", "ZG9", "D14", "D14", "MDY", "MDY", "MDY", "MDY",
"G3S", "G3S", "G3S", "J6Z", "J6Z", "J6Z", "6RU", "6RU", "6RU",
"6RU", "6RU", "6ZE", "6ZE", "6ZE")), class = "data.frame", row.names = c(NA, -51L))
在数据帧df中:
obs_id是估计变量的特定值的个人。 obs_value是此人观察到的值。 观察会一直持续到根据观察日期记录新观察为止。
观察结果的图显示如下:
library(plotly)
plot_ly(data = df, x = ~obs_date, y = ~obs_value,
type = 'scatter', mode = 'lines',
line = list(shape = "hvh"),
color = ~obs_id)
问题: 有没有办法在同一张图表中叠加/显示中值/平均观察值(在整个观察期内)?
答案 0 :(得分:0)
您可以尝试tidyverse
library(tidyverse)
df %>%
group_by(obs_id) %>%
mutate(start = min(obs_date),
end= max(obs_date),
Mean = mean(obs_value, na.rm = T)) %>%
ggplot(aes(obs_date, obs_value,color= obs_id)) +
geom_point() +
geom_segment(data= . %>% distinct(obs_id, start, end, Mean),
aes(x=start, xend = end, y = Mean, yend =Mean))
然后,您可以尝试使用library(plotly);ggplotly(the_plot)
根据您的评论,您可以尝试
df %>%
group_by(obs_id) %>%
mutate(start = min(obs_date),
end= max(obs_date),
Mean = mean(obs_value, na.rm = T)) %>%
ggplot(aes(obs_date, obs_value)) +
geom_point(aes(color= obs_id)) +
stat_summary(fun.y = "mean", geom="bar", alpha=0.2)