I am trying to create a scatter/line plot using plotly R and looping through a vector of car manufacturers. Unfortunately, the plot will only display the last manufacturer value. Could someone please show me where I am going wrong?
library(dplyr)library(plotly)df <- mpg %>% summarise(avg_displ = mean(displ), .by = c(manufacturer, year)) %>% pivot_wider(names_from = manufacturer, values_from = avg_displ)#list of manufacturersmanf_vec <- colnames(df[-1])p <- plot_ly(df, type = "scatter", mode = "lines+markers")for(manf in manf_vec){ p <- add_trace(p, x =~year, y = ~ df[[manf]])}p