I have an example dataframe with several unequal length vectors (i.e. some are 5 datapoints long, some are 3, etc. I have a loop that generates a ggplot for each column. However, I can't figure out how to dynamically shorten the plot when there are missing data.
Data Example:
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
Code so far:
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()
}
Plotting the first plot yields a good result:
But, plotting the second plot yields this short line:
How can I change my loop so that the second plot is this?:
Thank you in advance! Any help is appreciated
data
as an object name. 2) subset your argument passed to thedata
parameter. At the moment you are giving an entire column of dates to the plotting routine. - IRTFMna.omit(data)
to thegeom_line
call? - bob1