嗨,我的ggplot2中有两个参数,我想为所有的图创建一个列表对象。不知何故,它给了我一个错误
data = data.frame(date = rep(seq(as.Date('2020-01-01'),
as.Date('2020-01-07'),length.out =7),2),
METRIC_NAME = rep(c('a','b','c','d','e','f','g'),2),
METRIC_VALUE = seq(1,14,1), DIMENSION = rep(c('A','B'),7))
all_dimensions = unique(data$DIMENSION)
all_metric = unique(data$METRIC_NAME)
plot_function = function(x,y){
p = ggplot(subset(data, METRIC_NAME == x & DIMENSION == y),
aes(x = DATE, y = METRIC_VALUE,
color = DIMENSION_VALUE))+ geom_line() +
scale_x_date(breaks = date_breaks("week"),
labels = date_format("%d-%b"),
minor_breaks = date_breaks("1 day"))+
facet_wrap(dim~., scales = 'free')
return (p)}
lapply(all_metric, function(x){lapply(all_dimensions,
function(y)plot(x,y))} )错误是
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:发布于 2021-06-10 14:00:17
您可以为每个组组合拆分数据集,并使用map将绘图函数应用于每个组。
library(tidyverse)
data %>%
group_split(DIMENSION, METRIC_NAME) %>%
map(function(x){
ggplot(x,aes(x = date, y = METRIC_VALUE,
color = DIMENSION_VALUE))+ geom_line() +
scale_x_date(breaks = date_breaks("week"),
labels = date_format("%d-%b"),
minor_breaks = date_breaks("1 day"))+
facet_wrap(DIMENSION~., scales = 'free')
}) -> list_plots
list_plotshttps://stackoverflow.com/questions/67914901
复制相似问题