Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # setup data
- mycountries <- c("India", "Iran", "Iraq", "Italy")
- varnames <- c("lifeExp","pop","gdpPercap")
- Df2 <- gapminder::gapminder %>%
- dplyr::filter(country %in% mycountries) %>%
- tidyr::pivot_longer(cols = varnames, names_to = "Ecoregion", values_to = "Tpoint") %>%
- dplyr::mutate(Time = year, Species = country)
- Df2$Species <- factor(Df2$Species, levels = mycountries)
- # build plot
- p <- filter(Df2, Ecoregion == unique(Df2$Ecoregion)[1]) %>%
- plot_ly(
- x = ~Time, y = ~Tpoint, color = ~Species, type = "scatter", mode = "lines"
- ) %>%
- layout(
- title = "Ecoregion Line Plots",
- xaxis = list(title = "Time"),
- yaxis = list(title = "Tpoint"),
- updatemenus = list(
- list(
- buttons = list(
- list(
- method = "restyle",
- args = list(list(
- x = Df2[Df2$Ecoregion == "lifeExp", "Time"],
- y = Df2[Df2$Ecoregion == "lifeExp", "Tpoint"],
- color = Df2[Df2$Ecoregion == "lifeExp", "Species"],
- type = "scatter",
- mode = "lines"
- ), 0L),
- label = "lifeExp"),
- list(
- method = "restyle",
- args = list(list(
- x = Df2[Df2$Ecoregion == "pop", "Time"],
- y = Df2[Df2$Ecoregion == "pop", "Tpoint"],
- color = Df2[Df2$Ecoregion == "pop", "Species"],
- type = "scatter",
- mode = "lines"
- ), 1L),
- label = "pop"),
- list(
- method = "restyle",
- args = list(list(
- x = Df2[Df2$Ecoregion == "gdpPercap", "Time"],
- y = Df2[Df2$Ecoregion == "gdpPercap", "Tpoint"],
- color = Df2[Df2$Ecoregion == "gdpPerCap", "Species"],
- type = "scatter",
- mode = "lines"
- ), 2L),
- label = "gdpPercap")
- )
- )
- )
- )
- # Show the interactive plot
- p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement