我有一个时间序列数据,其中有多个以不同单位衡量的变量。这是每日数据。数据如下。 (示例数据)
structure(list(date = structure(18324:18329, class = "Date"),
x = c(-1805605.65336663, -217934.802608961, -1032002.23625031, 234816.624919304, 1321982.20108174, 104251.623282941), y = c(0.633729348424822, 0.244916933588684, 0.873351667076349, 0.552934182109311, 0.348864572821185, 0.197756679030135), z = c(3L, 5L, 5L, 6L, 5L, 6L)), class = "data.frame", row.names = c(NA, -6L
))
假设X以十亿卢比度量,Y是0到1之间的比率,Z是计数变量。我想在一段时间内在多个图表中绘制所有这些变量(最好使用facet_wrap)
答案 0 :(得分:0)
您可以使用以下代码
library(tidyverse)
library(lubridate)
df %>%
dplyr::mutate(date = ymd(date)) %>%
gather(key = "key", value = "value",-date) %>%
ggplot(aes(x=date, y=value)) + geom_line() + facet_wrap("key", scales = "free")
df %>%
dplyr::mutate(date = ymd(date)) %>%
gather(key = "key", value = "value",-date) %>%
ggplot(aes(x=date, y=value)) + geom_line() + theme_bw() +
facet_wrap(~key, scales = "free_y", ncol = 1,
strip.position = "left",
labeller=as_labeller(c(x = "Rs Billion", y = "Ratio", z = "Count variable (n)"))) +
ylab(NULL) +xlab("Date")+
theme(strip.background = element_blank(),
strip.placement = "outside")