I chose to use some census data for the town of Carmacks, Yukon, Canada. I plotted the same data twice using two approaches to plotly:

  1. Create a ggplot, then use the ggploty() function to generate the ploty graphic
  2. create the plot directly with the plot_ly() function
library(readr)
library(dplyr)
library(ggplot2)
library(plotly)

# LOad the data and then filter out a subset for class exercise
dat <- read_csv("C:/P_Teaching/P_SpainIntro/SpainIntro_Data/PopTidyData.csv")
sub1 <- dplyr::filter(dat, Sex!="Total" & Month=="December" & AgeClass=="All Ages")

Plotly graphic rendered with ggplotly()

g <- ggplot(data=sub1, aes(Year, CountIndividuals, color=Sex))+
    geom_point(stat="identity") + 
    geom_line() +
    ggtitle("Carmacks December Census")
ggplotly(g)

Plotly graphic rendered with plot_ly()

plot_ly(sub1, x = ~Year, y = ~CountIndividuals, color = ~Sex,
        type = 'scatter', mode = 'lines',
        text = ~paste("Population: ", CountIndividuals)) %>%
    layout(title = 'Carmacks December Census')