Search code examples
rr-plotly

How do i get the row values instead of the row names


I have following code and it doesnt work

df <- data.frame (
  Bikes = c("Canyon", "Santa", "Radon"),
  Tretlage = c(1,1,3),
  Kurbel = c(2,3,4),
  Winkel = c(4,5,3)
)


plot_ly(
      type = 'scatterpolar',
      mode = "closest",
      fill = 'toself'
    ) %>%
      add_trace(
        r = df[1,],
        theta = c("Tretlage", "Kurbel","Winkel"),
        showlegend = TRUE,
        mode = "markers",
        name = df[1,1]
)

and instead of typing in r every number like

r = c(1,2,4) 

i want to put in like "df" and it takes the values automatically, but somehow that doesnt happen. Does someone know why?

it should look like this:if r = c(1,2,4)


Solution

  • An option is to use a for loop

    p1 <- plot_ly(
      type = 'scatterpolar',
      mode = "closest",
      fill = 'toself'
    )
    
    for(i in seq_len(nrow(df))) {
         p1 <- p1  %>%
                 add_trace(
               r = unlist(df[i,-1]),
               theta = c("Tretlage", "Kurbel","Winkel"),
            showlegend = TRUE,
            mode = "markers",
            name = df[i,1]
          )
      }
    
    p1
    

    -output enter image description here