简体   繁体   中英

Shiny + ggplot2: plotting a number of lines in one graph based on user input

I have hundreds of TimeSeries lines, each corresponding to unique values of a set of parameters. I put all the data in one large dataframe. The data looks like this (containing 270 TimeSeries):

> beginning
  TimeSeriesID TimeSeries Par1 Par2 Par3 Par4 Par5
1            1   3936.693   51 0.05    1    1 True
2            1   3936.682   51 0.05    1    1 True
3            1   3945.710   51 0.05    1    1 True
4            1   3937.385   51 0.05    1    1 True
5            1   3938.050   51 0.05    1    1 True
6            1   3939.387   51 0.05    1    1 True

> end
        TimeSeriesID TimeSeries Par1  Par2 Par3 Par4  Par5
3600452          270    -16.090  190 0.025    5    5 False
3600453          270    -21.120  190 0.025    5    5 False
3600454          270    -14.545  190 0.025    5    5 False
3600455          270    -23.950  190 0.025    5    5 False
3600456          270     -4.390  190 0.025    5    5 False
3600457          270     -3.180  190 0.025    5    5 False

What I am trying to achieve is for the Shiny app to allow the user vary the parameters he wants, get the user input and plot all the TimeSeries that satisfy those values in one plot. Therefore the plot will have different number of lines displayed given the users' input - ranging from one (when all parameters are set to a specified value) to 270 (when no parameters are chosen, all TimeSeries are plotted).

I had no success so far, so there is nothing I can share that may help solve the problem, although I spent many days on-and-off the it. So far I have been trying to use reactivePlot() and specify the lines by adding geom_line() in ggplot2. Now I am trying to look into the aes() parameter whether there is a possibility achieve what I need. I have also read about converting data into long format by reshape2, but I am not sure that is what I need, since I am working with TimeSeries data.

Thank you in advance.

In the end I went for a base R solution. Not perfect, but suited my needs:

equityplot.IDs <- function()
  {
    bounds <- c(-6000, 100000) #c(min(sapply(eq.list, min)), max(sapply(eq.list, max)))
    colors <- rainbow(length(outputIDs()[[2]]))
    j <- 1
    indexy <- c(0, 6000)
    # Plot
    plot(NULL,xlim=indexy,ylim=bounds)
    for (i in 1:length(equitieslist))
    {
      if(i %in% outputIDs()[[2]])
      {
        profit <- rev(equitieslist[[i]][,1]) #$Profit1)
        lines(1:length(profit), profit, col=colors[j])
        j <- j + 1
      }
    }
  }

After more experimenting, currently working with this:

ggpokus <- function(n) {

  mymin <- function(N = n){
    m <- Inf
    for (i in 1:N)
    {
      g <- length(equitieslist[[i]][,1])
      if (g < m) {m <- g}
    }
    return (m)
  }

  mylength <- mymin()
  # t <- paste("qplot(1:", mylength, ", rev(equitieslist[[", 1, "]][,1])[1:", mylength, "], geom = \"line\")", sep = "")
  t <- paste("qplot(1:", mylength, ", rev(equitieslist[[", 1, "]][,1])[1:", mylength, "], geom = \"line\", ylim = c(0, 5000))", sep = "")
  cols <- rainbow(n)
  for (i in 1:n) {
    p <- paste("rev(equitieslist[[", i+1, "]][,1])[1:", mylength, "])", sep = "")
    c <- paste("\"", cols[i+1], "\"", sep = "") #  paste("cols[", i, "]", sep = "")
    t <- c(paste(t, " + geom_line(aes(y = ", p,", colour = ", c, ")", sep = ""))
  }
  # cat(t)
  # cat("\n")
  return (t)
}

options(expressions=10000)
z <- ggpokus(1619)

eval(parse(text=z))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM