2012-11-10 102 views
4

目前,我的代码最初可以让用户选择他们想要的数据集(两种选择)。然后根据他们选择的内容,出现数据集各个子集的其他绘图变量。这很好,除了我希望重复所有情节的情节,而不是像默认情况下那样分开。An R Shiny问题 - 在一张图上绘制多条线

我有一个默认绘图plot_Total,而数据集中的其他选项正在查看此特定的子集。所以只有一个分散是有道理的。

output$plot_Total <- reactivePlot(function() { 
    plot.new() 
    plot.window(xlim=c(1850,2020), ylim = c(0,5000000)) 
    axis(1) 
    axis(2) 
    title(main="Numbers over the years") 
    title(xlab="Year") 
    title(ylab="Number of people") 
    box() 
    points(dat$Year, dat$Total, col="red") 
    lines(dat$Year, dat$Total, col="red") 
    }) 

output$plot_subset1 <- reactivePlot(function() { lines(dat$Year, dat$subset1) }) 
output$plot_subset2 <- reactivePlot(function() { lines(dat$Year, dat$subset2) }) 

为什么这段代码片段不工作?它只是为每个(不需要的)图形创建空格,在其下面会显示“错误:plot.new尚未被调用”。如何指定将这些行添加到默认(plot_Total)图?

+3

你看过闪亮的主页上的例子吗?它似乎做你想要添加一个低级绘图命令到现有的情节:http://www.rstudio.com/shiny/ – Chase

回答

7

更新:通过玩弄上闪亮的网页图形中的代码,我意识到,我必须这样做:

output$plot_Total <- reactivePlot(function() { 
    plot.new() 
    plot.window(xlim=c(1850,2020), ylim = c(0,5000000)) 
    axis(1) 
    axis(2) 
    title(main="Numbers over the years") 
    title(xlab="Year") 
    title(ylab="Number of people") 
    box() 
    points(dat$Year, dat$Total, col="red") 
    lines(dat$Year, dat$Total, col="red") 
    if (input$RC) { lines(dat$Year, dat$dat)} 
    }) 

这不同于我原来的代码在两个方面。首先,在同一个反应图函数中,条件被添加为一行。其次,我创建了一个只包含子集RC的新数据框。这最初不是作为输入$ dat $ RC工作,但是当RC是它自己的数据框时,它作为输入$ RC工作。

点追逐转向我在正确的方向!