具有不等长度向量的R ggplot2打印循环

具有不等长度向量的R ggplot2打印循环,r,vector,ggplot2,plot,rstudio,R,Vector,Ggplot2,Plot,Rstudio,我有一个数据帧示例,其中有几个长度不等的向量(即,一些是5个数据点长,一些是3个,等等)。我有一个循环,为每列生成一个ggplot。但是,我不知道如何在缺少数据时动态缩短绘图 数据示例: date X1 X2 X3 1 1997-01-31 0.6094410 NA 0.5728303 2 1997-03-03 0.7741195 NA 0.0582721 3 1997-03-31 0.7269925 0.56

我有一个数据帧示例,其中有几个长度不等的向量(即,一些是5个数据点长,一些是3个,等等)。我有一个循环,为每列生成一个ggplot。但是,我不知道如何在缺少数据时动态缩短绘图

数据示例:

        date        X1        X2        X3
1 1997-01-31 0.6094410        NA 0.5728303
2 1997-03-03 0.7741195        NA 0.0582721
3 1997-03-31 0.7269925 0.5628813 0.8270764
4 1997-05-01 0.5471391 0.5381265 0.8678812
5 1997-05-31 0.8056487 0.4129166 0.6582061
迄今为止的代码:

vars <- colnames(data[-1])
plots <- list()

for (x in 1:length(vars)) {
  plot[[x]] <- ggplot(data = data, aes_q(x = data[, 1], y = data[, x + 1])) + 
    geom_line()
}

vars在指定y轴所需的列之前,
ggplot
将准备映射到整个数据帧。因此,如果您只需输入
ggplot(data,aes(x=date))
,您将获得该范围的空白绘图:

因此,如果不希望某个系列打印整个范围,则必须首先将数据集过滤到为要用于
y
值的列定义的行。例如,可以使用以下方法创建X2图:

temp <- data[complete.cases(data[c(1,3)]), c(1,3)]
ggplot(temp, aes(x = date, X2)) + geom_line()

要对所有变量执行此操作,这里有一种使用
dplyr
tidyr
purrr
的方法:

library(purrr); library(dplyr); library(tidyr)
plots <- data %>% 
  # Convert to long form and remove NA rows
  gather(var, value, -date) %>%
  drop_na() %>%

  # For each variable, nest all the available data
  group_by(var) %>%
  nest() %>%

  # Make a plot based on each nested data, where we'll use the
  #   data as the first parameter (.x), and var as the second
  #   parameter (.y), feeding those into ggplot.
  mutate(plot = map2(data, var, 
                     ~ggplot(data = .x, aes(date, value)) +
                       geom_line() +
                       labs(title = .y, y = .y)))

# At this point we have a nested table, with data and plots for each variable:
plots
# A tibble: 3 x 3
  var   data             plot    
  <chr> <list>           <list>  
1 X1    <tibble [5 x 2]> <S3: gg>
2 X2    <tibble [3 x 2]> <S3: gg>
3 X3    <tibble [5 x 2]> <S3: gg>

# To make this like the OP, we can extract just the plots part, with
plots <- plots %>% pluck("plot")
plots

plots[[1]]
plots[[2]] # or use `plots %>% pluck(2)`
plots[[3]]
library(purrr);library(dplyr);library(tidyr)
地块%
#转换为长格式并删除NA行
聚集(变量、值、日期)%>%
下拉菜单()%>%
#对于每个变量,嵌套所有可用数据
分组依据(var)%>%
嵌套()%>%
#根据每个嵌套数据绘制一个图,其中我们将使用
#数据作为第一个参数(.x),var作为第二个参数
#参数(.y),将这些参数输入ggplot。
突变(绘图=map2(数据,变量,
~ggplot(数据=.x,aes(日期,值))+
geom_线()+
实验室(标题=.y,y=.y)))
#此时,我们有一个嵌套表,其中包含每个变量的数据和曲线图:
阴谋
#一个tibble:3x3
var数据图
1 X1
2×2
3×3
#为了使这像OP一样,我们可以只提取绘图部分,使用
绘图%Pull(“绘图”)
阴谋
绘图[[1]]
绘图[[2]]#或使用'plots%>%pluck(2)`
绘图[[3]]

1)停止使用
数据
作为对象名称。2) 将参数子集传递给
数据
参数。此时,您正在为绘图例程提供一整列日期。如果将
na.omit(data)
添加到
geom\u行
调用,会发生什么情况?
library(purrr); library(dplyr); library(tidyr)
plots <- data %>% 
  # Convert to long form and remove NA rows
  gather(var, value, -date) %>%
  drop_na() %>%

  # For each variable, nest all the available data
  group_by(var) %>%
  nest() %>%

  # Make a plot based on each nested data, where we'll use the
  #   data as the first parameter (.x), and var as the second
  #   parameter (.y), feeding those into ggplot.
  mutate(plot = map2(data, var, 
                     ~ggplot(data = .x, aes(date, value)) +
                       geom_line() +
                       labs(title = .y, y = .y)))

# At this point we have a nested table, with data and plots for each variable:
plots
# A tibble: 3 x 3
  var   data             plot    
  <chr> <list>           <list>  
1 X1    <tibble [5 x 2]> <S3: gg>
2 X2    <tibble [3 x 2]> <S3: gg>
3 X3    <tibble [5 x 2]> <S3: gg>

# To make this like the OP, we can extract just the plots part, with
plots <- plots %>% pluck("plot")
plots

plots[[1]]
plots[[2]] # or use `plots %>% pluck(2)`
plots[[3]]