給定資料幀 df1 和 df2 如下:
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"), unit = structure(c(3L,
3L, 1L, 1L, 2L), .Label = c("", "%", "$"), class = "factor")), class = "data.frame", row.names = c(NA,
-5L))
使用下面的代碼,我可以回圈所有基于列的列df2,這意味著對于每一列,如果其型別相同,則將它們繪制在同一個圖上,最后用型別名稱保存它們。
library(ggplot2)
library(data.table)
df1$date <- as.Date(df1$date)
df1.m <- melt(df1, id = "date") # convert to long format
# 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)
print(g)
# ggsave(paste0(my_type,".png"))
}
現在我希望ylab()為每個圖重置,這意味著如果它的型別相同,則ylab()使用unitfrom設定它df2。
我嘗試下面的代碼,但它不起作用:
for(my_type in unique(df1.m2$type))
for (my_unit in unique(df1.m2$unit)){
g <- ggplot(data = df1.m2[df1.m2$type == my_type,],
aes(x=date, y = value, colour=variable))
ylab(my_unit)
geom_line(size = 1, alpha = 1)
print(g)
# ggsave(paste0(my_type,".png"))
}
我怎樣才能根據上面的代碼實作這一目標?在此表示衷心的感謝。
uj5u.com熱心網友回復:
不確定您對嵌套的for. 據我所知,您可以通過使用unique(unit)過濾資料集的 的作為y比例標簽來實作您想要的結果:
library(ggplot2)
for(my_type in unique(df1.m2$type)) {
d <- df1.m2[df1.m2$type == my_type,]
g <- ggplot(data = d,
aes(x=date, y = value, colour=variable))
geom_line(size = 1, alpha = 1)
labs(y = unique(d$unit))
print(g)
}



uj5u.com熱心網友回復:
這是一種替代方法,可以完全避免遍歷資料幀。
轉換
df1為長格式并重命名name為columns加入到
df2上columns改變列
type以包括單位繪制值與日期、顏色依據
columns和分面依據type進行一些主題調整以在每個面軸上顯示型別和單位
library(ggplot2) library(dplyr) library(tidyr) df1 %>% pivot_longer(2:6, names_to = "columns") %>% left_join(df2) %>% mutate(type = paste(type, unit)) %>% ggplot(aes(date, value)) geom_line(aes(color = columns, group = columns)) facet_grid(type ~ ., switch = 'y', scales = "free_y") theme(axis.title.y = element_blank(), strip.background = element_rect(fill = 'transparent'), strip.placement = 'outside', strip.text.y = element_text(angle=180))
結果:

uj5u.com熱心網友回復:
for(my_type in unique(df1.m2$type)) {
g <- ggplot(
data = df1.m2[type == my_type],
aes(x=date, y = value, colour=variable)
)
scale_y_continuous(labels = function(x) {
if(my_type == "price") {
paste("$", x)
} else if(my_type == "pct_change") {
paste(x, "%")
} else {
x
}
})
geom_line(size = 1, alpha = 1)
print(g)
# ggsave(paste0(my_type,".png"))
}
或者使用 if else 代替開關
scale_y_continuous(labels = function(x) {
switch(my_type, "price" = paste("$", x), "pct_change" = paste(x, "%"), x)
})

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/364778.html
