Advertisement
Guest User

Untitled

a guest
Nov 27th, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.93 KB | Software | 0 0
  1. # setup data
  2. mycountries <- c("India", "Iran", "Iraq", "Italy")
  3. varnames <- c("lifeExp","pop","gdpPercap")
  4. Df2 <- gapminder::gapminder %>%
  5.   dplyr::filter(country %in% mycountries) %>%
  6.   tidyr::pivot_longer(cols = varnames, names_to = "Ecoregion", values_to = "Tpoint") %>%
  7.   dplyr::mutate(Time = year, Species = country)
  8.  
  9. Df2$Species <- factor(Df2$Species, levels = mycountries)
  10.  
  11. # build plot
  12. p <- filter(Df2, Ecoregion == unique(Df2$Ecoregion)[1]) %>%
  13.   plot_ly(
  14.     x = ~Time, y = ~Tpoint, color = ~Species, type = "scatter", mode = "lines"
  15.   ) %>%
  16.   layout(
  17.     title = "Ecoregion Line Plots",
  18.     xaxis = list(title = "Time"),
  19.     yaxis = list(title = "Tpoint"),
  20.     updatemenus = list(
  21.       list(
  22.         buttons = list(
  23.           list(
  24.             method = "restyle",
  25.             args = list(list(
  26.               x = Df2[Df2$Ecoregion == "lifeExp", "Time"],
  27.               y = Df2[Df2$Ecoregion == "lifeExp", "Tpoint"],
  28.               color = Df2[Df2$Ecoregion == "lifeExp", "Species"],
  29.               type = "scatter",
  30.               mode = "lines"
  31.             ), 0L),
  32.             label = "lifeExp"),
  33.           list(
  34.             method = "restyle",
  35.             args = list(list(
  36.               x = Df2[Df2$Ecoregion == "pop", "Time"],
  37.               y = Df2[Df2$Ecoregion == "pop", "Tpoint"],
  38.               color = Df2[Df2$Ecoregion == "pop", "Species"],
  39.               type = "scatter",
  40.               mode = "lines"
  41.             ), 1L),
  42.             label = "pop"),
  43.           list(
  44.             method = "restyle",
  45.             args = list(list(
  46.               x = Df2[Df2$Ecoregion == "gdpPercap", "Time"],
  47.               y = Df2[Df2$Ecoregion == "gdpPercap", "Tpoint"],
  48.               color = Df2[Df2$Ecoregion == "gdpPerCap", "Species"],
  49.               type = "scatter",
  50.               mode = "lines"
  51.             ), 2L),
  52.             label = "gdpPercap")
  53.         )
  54.       )
  55.     )
  56.   )
  57.  
  58. # Show the interactive plot
  59. p
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement