Selecting the values of a row for r in a scatterpolar

I'm trying to build a scatterplot and have problems with assigning the correct r value. Here you have a glimpse at my dataset:

As you see, the first column represents the names of bikes, columns 2 to 11 show different geometry data. I want the scatterpolar to look like this:

plot_ly(
 type = 'scatterpolar',
 mode = 'markers',
 fill = "toself",
 r = c(435, 65.5, 629, 1193, 434, 440, 73.5, 635, 100, 32),
 theta = c("Kettenstrebe", "Lenkwinkel","Oberrohr","Radstand","Reach","Sattelrohr","Sitzwinkel","Stack","Steuerrohr",
                 "Tretlagerabsenkung"),
 showlegend = TRUE,
 name = ComparisonTable[1,1])

How can I define the r value in a way, that it automatically shows the values of the desired row, without me putting in every and each value?

I tried the following code, but without success:

plot_ly(
  type = 'scatterpolar',
  mode = 'markers',
  fill = "toself",
  r = as.matrix(ComparisonTable[1]),
  theta = c("Kettenstrebe", "Lenkwinkel","Oberrohr","Radstand","Reach","Sattelrohr","Sitzwinkel","Stack","Steuerrohr",
                  "Tretlagerabsenkung"),
  showlegend = TRUE,
  name = ComparisonTable[1,1])

I am happy to get your expertise on the problem!

In plotly you can either provide a data frame or the individual vectors:

data = mydataframe,
r = ~Bikes

or

r = mydataframe$Bikes

Thanks!,
sadly it did not work i get
"Error: Tibble columns must have compatible sizes. * Size 10: Column theta. * Size 19: Column r. i Only values of size one are recycled."

I know its because i got 19 Bikes, but i do not want the bikes i just want the values in the table. And somehow that doesnt work.

this is what i get when i put the code:

plot_ly(
  type = 'scatterpolar',
  mode = 'markers',
  fill = "toself",
  r = ComparisonTable,
  theta = c("Kettenstrebe", "Lenkwinkel","Oberrohr","Radstand","Reach","Sattelrohr","Sitzwinkel","Stack","Steuerrohr",
                  "Tretlagerabsenkung"),
  showlegend = TRUE,
  name = ComparisonTable[1,1])

i made a small version of the problem

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?

I think the trick is to transform the data frame using tidyr::pivot_longer before visualizing.

library(tidyr)

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

# pivot all columns except for Bikes
df <- tidyr::pivot_longer(df, !Bikes)

# tibble [9 x 3] (S3: tbl_df/tbl/data.frame)
#  $ Bikes: chr [1:9] "Canyon" "Canyon" "Canyon" "Santa" ...
#  $ name : chr [1:9] "Tretlage" "Kurbel" "Winkel" "Tretlage" ...
#  $ value: num [1:9] 1 2 4 1 3 5 3 4 3

plot_ly(
  df,
  type = 'scatterpolar',
  mode = "closest",
  fill = 'toself'
) %>%
  add_trace(
    r = ~value,
    theta = ~name,
    showlegend = TRUE,
    mode = "markers",
    name = ~Bikes
  )
1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.