Plot multiple columns saved in data frame with no x

123 Views Asked by At

My problem is multifaceted.

  1. I would like to plot multiple columns saved in a data frame. Those columns do not have an x variable but would essentially be 1 to 101 consistent for all. I have seen that I can transfer them into long format but most ggplot options require an X. I tried zoo which does what I want it to, but the x-label is all jumbled and I am not aware of how to fix it. (Example of data below, and plot)
    df <- zoo(HIP_131_Y0_LC_walk1[1:9])
    plot(df)
  1. I have multiple data frames saved in a list so ultimately would like to run a function and apply to all. The zoo function solves step one but I am not able to apply to all the data frames in the list.
    graph<-lapply(myfiles,function(x) zoo(x) )
    print(graph)
  1. Ideally I would like to also mark minimum and maximum, which I am aware can be done with ggplot but not zoo.

Thank you so much for your help in advance

Data

Sample plot

1

There are 1 best solutions below

0
On

Assuming that the problem is overlapped panel names there are numerous solutions to this:

  1. abbreviate the names using abbreviate. We show this for plot.zoo and autoplot.zoo .
  2. put the panel name in the upper left. We show this for plot.zoo using a custom panel.
  3. Use a header on each panel. We show this using xyplot.zoo and using ggplot.

The examples below use the test input in the Note at the end. (Next time please provide a complete example including all input in reproducible form.)

The first two examples below abbreviates the panel names and using plot.zoo and autoplot.zoo (which uses ggplot2). The third example uses xyplot.zoo (which uses lattice). This automatically uses headers and is probably the easiest solution.

library(zoo)

plot(z, ylab = abbreviate(names(z), 8))

library(ggplot2)
zz <- setNames(z, abbreviate(names(z), 8)) 
autoplot(zz)

library (lattice)
xyplot(z)

(click on plots to see expanded; continued after plots) screenshot

screenshot

This fourth example puts the panel names in the upper left of the panel themselves using plot.zoo with a custom panel.

pnl <- function(x, y, ..., pf = parent.frame()) {
   legend("topleft", names(z)[pf$panel.number], bty = "n", inset = -0.1)
   lines(x, y)
}
plot(z, panel = pnl, ylab = "")

(click on plot to see it expanded) screenshot

We can also get headers with autoplot.zoo similar to in lattice above.

library(ggplot2)
autoplot(z, facets = ~ Series, col = I("black")) + 
  theme(legend.position = "none")

(click to expand; continued after graphics) screenshot

List

If you have a list of vectors L (see Note at end for a reproducible example of such a list) then this will produce a zoo object:

do.call("merge", lapply(L, zoo))

Note

Test input used above.

library(zoo)

set.seed(123)
nms <- paste0(head(state.name, 9), "XYZ") # long names
m <- matrix(rnorm(101*9), 101, dimnames = list(NULL, nms))
z <- zoo(m)

L <- split(m, col(m)) # test list using m in Note