[英]Plotting by ggplot in R
I have a time series data for different group components. 我有一个针对不同小组成员的时间序列数据。 Each group ID with its various time stamps (given as Date) has an hypo and hyper response data. 每个带有不同时间戳的组ID(给定为Date)都有一个hypo和hyper响应数据。 I would like to plot the time series for each of this group by facet (ggplot) for both (1) Group ID and also by response ie (2) Hyper and Hypo response so that the picture by response is one top of another. 我想通过(1)组ID以及响应(即(2)Hyper和Hypo响应)的构面(ggplot)绘制此组中每个组的时间序列,以使响应的画面彼此顶峰。 Any help is appreciated. 任何帮助表示赞赏。
A demo data set and what I have done so far is given below. 下面给出了一个演示数据集以及我到目前为止所做的事情。
set.seed(1)
tdat <- data.frame(Group = rep(paste0("GroupID-", c("A","B")),
each = 100),
Date = rep(seq(Sys.Date(), by = "1 day", length = 100), 2),
Fitted = c(cumsum(rnorm(100)), cumsum(rnorm(100))),
Signif = rep(NA, 200))
tdat <- transform(tdat, Hyper = Fitted + 1.5, Hypo = Fitted - 1.5)
## select 1 region per Site as signif
take <- sample(10:70, 2)
take[2] <- take[2] + 100
tdat$Signif[take[1]:(take[1]+25)] <- tdat$Fitted[take[1]:(take[1]+25)]
tdat$Signif[take[2]:(take[2]+25)] <- tdat$Fitted[take[2]:(take[2]+25)]
And the data frame looks like this - 数据框看起来像这样-
> head(tdat)
Group Date Fitted Signif Hyper Hypo
1 GroupID-A 2017-04-18 -0.6264538 NA 0.8735462 -2.1264538
2 GroupID-A 2017-04-19 -0.4428105 NA 1.0571895 -1.9428105
3 GroupID-A 2017-04-20 -1.2784391 NA 0.2215609 -2.7784391
4 GroupID-A 2017-04-21 0.3168417 NA 1.8168417 -1.1831583
5 GroupID-A 2017-04-22 0.6463495 NA 2.1463495 -0.8536505
6 GroupID-A 2017-04-23 -0.1741189 NA 1.3258811 -1.6741189
The time series is given by Date. 时间序列由日期给出。
The data I have plotted is given below. 我绘制的数据如下。 However my real data has more group ID's and I really want one picture for each group ID with splitting the image for Hyper and Hypo response. 但是,我的真实数据具有更多的组ID,我真的想要每个组ID一张图片,并将图像拆分为Hyper和Hypo响应。
library(ggplot2)
ggplot(tdat, aes(x = Date, y = Fitted, group = Group)) +
geom_line() +
geom_line(mapping = aes(y = Hyper), lty = "dashed") +
geom_line(mapping = aes(y = Hypo), lty = "dashed") +
geom_line(mapping = aes(y = Signif), lwd = 1.3, colour = "red") +
facet_wrap( ~ Group)
Again any help is appreciated. 再次感谢任何帮助。
Thanks 谢谢
If you will reshape
your data with reshape2
or tidyr
or data.table
and convert wide to long: 如果您将使用reshape2
或tidyr
或data.table
reshape
数据并将宽转换为长:
library(reshape2)
tdat2<-melt(tdat,id.vars = c("Group","Date","Signif","Fitted"))
ggplot(tdat2, aes(x = Date, y = value, group = Group)) +
geom_line() +
geom_line(mapping = aes(y = Signif), lwd = 1.3, colour = "red") +
facet_wrap( variable~ Group)
How about something like this, using geom_ribbon
to show the Hyper and Hypo values: 使用geom_ribbon
显示Hyper和Hypo值,这样的事情怎么样:
tdat %>%
ggplot(aes(Date, Fitted)) +
geom_line(lty = "dashed") +
geom_line(aes(y = Signif), lwd = 1.3, color = "red") +
geom_ribbon(aes(ymin = Hypo, ymax = Hyper, group = Group), alpha = 0.2) +
facet_grid(Group ~ .) +
theme_light()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.