Two graphs in one plot


#1

Hallo,

I am new to shiny and have finished a few tutorials but one thing is still unclear for me. How can i add two graphs to one plot? The user must be able two chose different years to plot over each other to see the difference in temperature changes. I tried it with a switch but that only lets me plot one graph at a time.

Example:
the user can choose from the following years 2001,2002,2003,2004 and 2005.If the user chooses 2002 and 2005. The data from that years must be plotted over each other.

Currently it is possible for me to ask the user to choose the wanted years and link the data to the wanted years. The problem is setting up the code that the wanted years are plotted. The plots that i have made with shiny are all with the normal plot code, it has not yet been possible for met to make a plot in shiny with ggplot.

Can someone tell me how it is possible or point me to a post which presents the possibilities?

Thank you for your time.

Regards


#2

Hey, there’s plenty of examples out there. You can take a look at patchwork if you want to go the ggplot way. Here is a simple example creating a function that allows you having two (old fashion) plots on the same device, the key is on the par function:

rm(list = ls())

# Some example data
set.seed(12)
dat <- data.frame(
  year = rep(2010:2015, 50),
  x    = rnorm(length(rep(2010:2015, 50)))
)


# A function that creates two plots, you can define this function
# in your -ui.R- file
twoplots <- function(dat, by, cols) {
  
  # We change the plotting parameters
  oldpar <- par(no.readonly = TRUE) # This stores the current parameters
  on.exit(par(oldpar))              # This makes sure to restore them once we are done
  par(mfrow = c(1,2))               # This sets the new parameters: 2 columns (plots)
  
  # Plotting the first
  with(dat,
       plot(x[year == by[1]], type="b", col = cols[1])
       )
  
  # Plotting the second
  with(dat,
       plot(x[year == by[2]], type="b", col = cols[2])
       )
  
  
}

# Suppose that the user picked years 2011 and 2014
years <- c(2011, 2014)
cols  <- c("tomato", "steelblue")

# Calling the function!
twoplots(dat, years, cols)

HIT


#3

Plotting two ggplots next to each other—provided facetting is insufficient—can be done with gridExtra::grid.arrange, which gives very fine-grained control of how plots are combined, e.g.

library(ggplot2)

gridExtra::grid.arrange(
    ggplot(mtcars, aes(wt, mpg)) + geom_point(), 
    ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point(), 
    nrow = 1
)

This actually combines both plots into a single image. With Shiny, technically that’s not completely necessary, as you can choose how figures are displayed, but to give an answer that demonstrates what that would look like for you, you’d need to edit to add a reprex.


#4

It seems to me like you do not want to separate plots but rather just two lines on the same graph? If that is the case, the easiest way to do this is to put your data into a single data frame and define a variable that distinguishes what year the data is from. Then in your ggplot call you can specify color = year (assuming you named the column “year”) in your aes. For example:

 # stealing the sample data from @gvegayon and modifying it slightly
set.seed(12)
dat <- data.frame(
  year = rep(2010:2015, 50),
  y    = rnorm(length(rep(2010:2015, 50))),
  x = rep(1:50, 6)
) 

dat %>% 

  #filter to only two years
  filter(year %in% c(2011, 2014)) %>%
  
   # change the year column to a character to make the legend discrete instead of continuous
  mutate(year = as.character(year)) %>% 

  # plot the data
  ggplot(aes(x, y, color = year)) +
    geom_point() +
    geom_line()

That would give you this:

Now obviously this will not look so jumbled unless your temperature data is all over the place.

I hope this helps!


#5

Awesome thanks for the tips. I will give it a go. If i have any questions I wil let you know.


#6

Thanks again. it is working now