0

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

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • `df$x = 1:nrow(df)` - now you have an `x` column! And yes, I'd suggest transforming into long format to use ggplot. – Gregor Thomas Oct 09 '22 at 18:37
  • 1
    (Or, for all your data frames in the list, `myfiles <- lapply(myfiles, function(d) {d$x = 1:nrow(d); return(d)})`) – Gregor Thomas Oct 09 '22 at 18:39
  • @GregorThomas I tried what you mentioned and now I am having some issues with the plot function. `plots <- lapply(myfiles2, function(d) ggplot(d%>% mutate(joint = case_when( str_detect(series, "Ankle") ~ "Ankle", str_detect(series, "Knee") ~ "Knee", str_detect(series, "HipA") ~ "Hip")), aes(x,value)) + geom_line(aes(colour = series)) + facet_grid(.~joint))` – Edgar Garcia Oct 09 '22 at 19:50
  • @GregorThomas I figured it out, thanks again. Any idea on how to add min and max? – Edgar Garcia Oct 09 '22 at 20:12
  • I don't know a built-in way to do that--I'd summarize the each data frame to add it. Maybe you can find a good starting place [in these answers](https://stackoverflow.com/q/51697870/903061). If you need help writing code, you should make your example reproducible, sharing a little bit of sample data in a copy/pasteable way. No one can test code on a picture of a table. – Gregor Thomas Oct 10 '22 at 01:49
  • What does it mean to mark the minimum and maximum? The minimum and maximum of what and what does marking mean? – G. Grothendieck Oct 10 '22 at 13:06

1 Answers1

1

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
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341