루프 열과 플롯 정의는 동일한 유형에서 열 동일한 줄거리를 사용하여 ggplot2

0

질문

주 dataframes 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ý
..................................................................................................................