Shiny + ggplot2:根据用户输入在一个图中绘制多条线 [英] Shiny + ggplot2: plotting a number of lines in one graph based on user input

查看:328
本文介绍了Shiny + ggplot2:根据用户输入在一个图中绘制多条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数百个TimeSeries行,每行对应于一组参数的唯一值。我把所有的数据放在一个大的数据框中。数据看起来像这样(包含270个TimeSeries):

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

我试图实现的是Shiny应用程序允许用户更改他想要的参数,获取用户输入并绘制所有TimeSeries在一个图中满足这些值。因此,在给定用户输入的情况下,图表会显示不同的行数 - 从一个(当所有参数设置为指定值时)到270(当没有选择参数时,绘制所有TimeSeries)。

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).

到目前为止,我没有取得成功,所以我没有任何可以分享的东西可以帮助解决问题,尽管我花了很多天时间来解决问题。到目前为止,我一直试图使用 reactivePlot(),并通过在ggplot2中添加 geom_line()来指定行。现在我试图查看 aes()参数是否有可能实现我所需要的。我还读过关于通过reshape2将数据转换为长格式的内容,但我不确定这是我需要的,因为我正在使用TimeSeries数据。

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.

谢谢前进。

推荐答案

最后,我去了一个基本的R解决方案。不完美,但符合我的需求:

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))

这篇关于Shiny + ggplot2:根据用户输入在一个图中绘制多条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆