1

What I am trying to do is create a shiny app that plots my x and y for each obs on a scatter plot as it cycles through a time variable(t).

grph dataframe:

id x y t
1  3 8 3 
2  6 2 8   
3  2 6 1 
1  9 4 5  

server.R

library(shiny)
library(ggplot2)

function(input, output) {
  #Store data into df for plotting
  df <- data.frame(grph, header = TRUE)

  #Plot output
  output$plot <- renderPlot({
    dt<-cbind(df,Ftime=as.numeric(df$t))
    dt<-dt[dt$Ftime==input$t,]

    if(nrow(dt)==0) ggplot(df, aes(x,y))
    else ggplot(dt,aes(x,y))+geom_point()
  })
}

ui.R

library(shiny)
library(ggplot2)

fluidPage(
  #Sidebar header
  headerPanel('Demo'),
  #Sidebar panel
  sidebarPanel(
    #Animated sidebar (time converted to mins)
    sliderInput("t", "Time", 
                min = 0,
                max = 1440,
                value = 0, step=1,
                animate=
                  animationOptions(interval=1000, loop=TRUE))
  ),
  #Export graph to main panel
  mainPanel(
    plotOutput('plot')
  )
)

It is working for the most part but I currently have two problems: (1) I am having trouble stabilizing my scatter plot since it is constantly changing my max and min for both x and y on the graph and (2) Every now and then it will blur out the graph. Here is an example

t00T
  • 25
  • 3
  • So what exactly is the question? Are the full extent of the x and y values known at the start of the application? There's nothing in your example here that shows why the data is changing. A more [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be helpful. – MrFlick Aug 21 '17 at 18:54
  • @MrFlick My question is, how can I stabilize the graphs so x and y do not constantly change? The gif I provided shows my max and min for x and y constantly changing so you can't get a frame by frame reference on how points are moving since the graphs are not stable. – t00T Aug 21 '17 at 19:06

1 Answers1

1

(1) I am having trouble stabilizing my scatter plot since it is constantly changing my max and min for both x and y on the graph

You can use the same df to set the bounds, and then provide the subset dataset within geom_point.

ggplot(df,aes(x,y)) + geom_point(data = dt)

Alternatively, you can use coord_cartesian() and pass the min/max for the full dataset:

ggplot(dt, aes(x,y)) + 
  geom_point() +
  coord_cartesian(xlim = c(min(df$x), max(df$x)),
                  ylim = c(min(df$y), max(df$y)))

(2) Every now and then it will blur out the graph.

Your plotting is not able to keep up with the rate of animation. You can adjust the interval.

animationOptions(interval = 1000, loop = FALSE, playButton = NULL,
pauseButton = NULL)

# Animation with custom interval (in ms) to control speed, plus looping
    sliderInput("animation", "Looping Animation:", 1, 2000, 1, step = 10, 
                animate=animationOptions(interval=300, loop=T))
Eric Watt
  • 3,180
  • 9
  • 21
  • Okay, adjusting the animation options solved the problem with the blurry graphs! I am still having trouble with setting the boundaries though. Using geom_point(data = dt) did not stabilize it. Alternatively, if I want to use the coord_cartesian(), where should I input that? – t00T Aug 21 '17 at 19:32
  • `geom_point(data = dt)` will only work if you change the data argument in `ggplot` to be `df` instead of `dt`. For `coord_cartesian` you add it after `geom_point`. – Eric Watt Aug 21 '17 at 19:39
  • I edited the answer to make `coord_cartesian` clearer. – Eric Watt Aug 21 '17 at 19:42