Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11631

looping through plotly r using add_trace

$
0
0

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

Viewing all articles
Browse latest Browse all 11631

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>