提问人:GrBa 提问时间:10/24/2023 最后编辑:GrBa 更新时间:10/24/2023 访问量:39
在 ggplot 图的轴名称中使用数据帧列表中的数据帧名称
Using a dataframe name from the dataframes list in the axis name in a ggplot plot
问:
我有一个数据框列表
在此基础上,我准备图表ggplot
co_gpl <- lapply(co_lst, function(x) {
ggplot(x) +
geom_line (aes (x = `Czas`, y = get(names(x[grep("Temp. zasilania", colnames (x))])) ), color = "red3", size = 0.3) +
geom_line (aes (x = `Czas`, y = get(names(x[grep("Temp. powrotu", colnames (x))]))), color = "blue3", size = 0.3) +
scale_y_continuous(limits = c(min.T.pow, max.T.zas)) +
scale_x_datetime(date_labels = format ("%H:%M"),
minor_breaks = "1 hour") +
labs (y = paste0("", "/Temperatura [°C]")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
legend.position = "top",
legend.justification = "center")
})
ggp <- wrap_plots (co_gpl, nrow = 1, ncol = 3)
我希望名称 dataframe 出现在 x 轴标题中(分别为 A5、B5、K1)。另一种选择是让每个图表都有一个包含其数据框名称的副标题。
添加不会执行所需的操作names(x)
co_gpl <- lapply(co_lst, function(x) {
ggplot(x) +
geom_line (aes (x = `Czas`, y = get(names(x[grep("Temp. zasilania", colnames (x))])) ), color = "red3", size = 0.3) +
geom_line (aes (x = `Czas`, y = get(names(x[grep("Temp. powrotu", colnames (x))]))), color = "blue3", size = 0.3) +
scale_y_continuous(limits = c(min.T.pow, max.T.zas)) +
scale_x_datetime(date_labels = format ("%H:%M"),
minor_breaks = "1 hour") +
labs (y = paste0(names(x), "/Temperatura [°C]")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
legend.position = "top",
legend.justification = "center")
})
编辑
A5 <- data.frame( Time = c(as.POSIXct('2023-07-26 14:30:01'), as.POSIXct('2023-07-26 15:00:00'), as.POSIXct('2023-07-26 15:30:00'), as.POSIXct('2023-07-26 16:00:00'),as.POSIXct('2023-07-26 16:30:00')),
`[A5] zuzycie` = rnorm(5, 1, 1),
`[A5] temp` = rnorm(5, 10, 1),
`[A5] level` = rnorm(5, 20, 10)
)
B5 <- data.frame( Time = c(as.POSIXct('2023-07-26 14:30:01'), as.POSIXct('2023-07-26 15:00:00'), as.POSIXct('2023-07-26 15:30:00'), as.POSIXct('2023-07-26 16:00:00'),as.POSIXct('2023-07-26 16:30:00')),
`[B5] zuzycie` = rnorm(5, 1, 1),
`[B5] temp` = rnorm(5, 10, 1),
`[B5] level` = rnorm(5, 20, 10)
)
K1 <- data.frame( Time = c(as.POSIXct('2023-07-26 14:30:01'), as.POSIXct('2023-07-26 15:00:00'), as.POSIXct('2023-07-26 15:30:00'), as.POSIXct('2023-07-26 16:00:00'),as.POSIXct('2023-07-26 16:30:00')),
`[K1] zuzycie` = rnorm(5, 1, 1),
`[K1] temp` = rnorm(5, 10, 1),
`[K1] level` = rnorm(5, 20, 10)
)
co_lst2 <- list (A5=A5, B5=B5, K1=K1)
co_gpl <- lapply(co_lst2, function(x) {
ggplot(x) +
geom_line (aes (x = `Time`, y = get(names(x[grep("temp", colnames (x))])) ), color = "red3", size = 0.3) +
labs (y = paste0("", "/Temperatura [°C]")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
legend.position = "top",
legend.justification = "center")
})
ggp <- wrap_plots (co_gpl, nrow = 1, ncol = 3)
ggp
答:
1赞
Michael M
10/24/2023
#1
像这样的片段似乎是非常糟糕的风格。get(names(x[grep("Temp. zasilania", colnames (x))]))
但要回答这个问题:如果您需要访问列表名称,您可以使用
lapply(names(co_lst), function(nm) {
ggplot(co_lst[[nm]], ...) +
ggtitle(nm)
}
)
编辑:摆脱get()
-> .data[[]]
library(ggplot2)
library(patchwork)
A5 <- data.frame(
Time = as.POSIXct(
c('2023-07-26 14:30:01',
'2023-07-26 15:00:00',
'2023-07-26 15:30:00',
'2023-07-26 16:00:00',
'2023-07-26 16:30:00')
),
"[A5] zuzycie" = rnorm(5, 1, 1),
"[A5] temp" = rnorm(5, 10, 1),
"[A5] level" = rnorm(5, 20, 10),
check.names = FALSE
)
B5 <- data.frame(
Time = as.POSIXct(
c('2023-07-26 14:30:01',
'2023-07-26 15:00:00',
'2023-07-26 15:30:00',
'2023-07-26 16:00:00',
'2023-07-26 16:30:00')
),
"[B5] zuzycie" = rnorm(5, 1, 1),
"[B5] temp" = rnorm(5, 10, 1),
"[B5] level" = rnorm(5, 20, 10),
check.names = FALSE
)
K1 <- data.frame(
Time = as.POSIXct(
c('2023-07-26 14:30:01',
'2023-07-26 15:00:00',
'2023-07-26 15:30:00',
'2023-07-26 16:00:00',
'2023-07-26 16:30:00')
),
"[K1] zuzycie" = rnorm(5, 1, 1),
"[K1] temp" = rnorm(5, 10, 1),
"[K1] level" = rnorm(5, 20, 10),
check.names = FALSE
)
co_lst2 <- list(A5 = A5, B5 = B5, K1 = K1)
co_gpl <- lapply(co_lst2, function(x) {
ggplot(x) +
geom_line(
aes(x = `Time`, y = .data[[grep("temp", colnames(x), value = TRUE)]]),
color = "red3",
size = 0.3
) +
labs(y = "/Temperatura [°C]") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
legend.position = "top",
legend.justification = "center")
})
ggp <- wrap_plots (co_gpl, nrow = 1, ncol = 3)
ggp
编辑 2:更像 ggplot
X <- list(A5 = A5, B5 = B5, K1 = K1) |>
lapply(pivot_longer, -Time) |>
bind_rows(.id = "source") |>
mutate(name = factor(substring(name, 6)))
ggplot(X, aes(x = Time, y = value, group = name, color = name)) +
geom_line() +
facet_wrap("source") +
labs(color = "Variable")
# Or even
ggplot(X, aes(x = Time, y = value)) +
geom_line(color = "red") +
facet_grid(name ~ source)
评论
co_lst
purrr::imap
mapply