环列和情节制的相同类型列在相同的情节使用ggplot2

0

的问题

鉴于数据帧 df1df2 如下:

df1:

df1 <- structure(list(date = structure(c(1L, 2L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 3L), .Label = c("2021/1/31", "2021/2/1", "2021/2/10", 
"2021/2/2", "2021/2/3", "2021/2/4", "2021/2/5", "2021/2/6", "2021/2/7", 
"2021/2/8", "2021/2/9"), class = "factor"), value1 = c(9.76, 
9.76, 9.88, 9.31, 9.71, 9.56, 9.27, 9.22, 9.21, 9.08, 8.78), 
    value2 = c(6.84, 6.88, 6.95, 6.65, 6.94, 6.85, 6.66, 6.66, 
    6.6, 6.5, 6.25), value3 = c(6.33, 6.21, 6.31, 6.2, 6.56, 
    6.36, 6.36, 6.25, 6.1, 6.02, 5.76), value4 = c(10.68, 10.91, 
    11, 10.49, 10.8, 10.5, 10.2, 9.85, 10.03, 9.8, 9.51), value5 = c(7.77, 
    7.84, 7.83, 7.44, 7.83, 7.77, 7.6, 7.46, 7.46, 7.39, 7.29
    )), class = "data.frame", row.names = c(NA, -11L))

df2:

df2 <- structure(list(type = structure(c(2L, 2L, 3L, 3L, 1L), .Label = c("pct_change", 
"price", "quantity"), class = "factor"), columns = structure(1:5, .Label = c("value1", 
"value2", "value3", "value4", "value5"), class = "factor")), class = "data.frame", row.names = c(NA, 
-5L))

绘图时间序列 df1:

library(ggplot2)
library(data.table)
df1$date <- as.Date(df1$date)
df1.m <- melt(df1, id = "date")  # convert to long format
ggplot(data = df1.m,
       aes(x=date, y = value, colour=variable)) +
       geom_line(size = 1, alpha = 1) 
ggsave("df1.png")

出:

enter image description here

现在我希望循环所列但据 df2,这意味着每 columns如果其 type 都是相同的,那么阴谋它们在同一地块和最后保存他们的名字 type.

对于数据集 df1我们最终将产生的三个地块: price.png, quantity.pngpct_change.png.

我怎么可能实现,基于上述代码? 真诚的感谢在前进。

ggplot2 r
2021-11-24 04:40:27
1

最好的答案

2

我认为这样的事情应该做的你想要什么。 (但目前我跑进一个 错误与ggsave 其中应当固定给我用的ggplot2 3.3.5?) 我们希望它可以作为其他人。

# add "type" variable to df1.m
df1.m2 = merge(df1.m, df2, by.x = "variable", by.y = "columns")

# for each "type", filter the data to that type, plot, and save    
for(my_type in unique(df1.m2$type)) {
  g <- ggplot(data = df1.m2[df1.m2$type == my_type,],
       aes(x=date, y = value, colour=variable)) +
  geom_line(size = 1, alpha = 1) 
  ggsave(paste0(my_type,".png"))
}
2021-11-24 05:16:52

谢谢你,我用的 ggplot2 3.3.5 因为我能够拯救地无任何错误。
ah bon

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................