Can I recreate this polar coordinate spider chart in plotly?

后端 未结 3 1325
自闭症患者
自闭症患者 2021-02-04 06:18

I\'m having a bit of difficulty figuring out how to recreate the following graphic of a spider (or radar) chart, using plotly. Actually, I can\'t even recreate it in the most re

3条回答
  •  伪装坚强ぢ
    2021-02-04 06:51

    I've made some progress with this, by faking it. Polar coords, seem to just hate me:

    data:

    df <- d <- structure(list(Year = c("2015", "2015", "2015", "2015", "2015", 
    "2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015", 
    "2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015", 
    "2015", "2015", "2015", "2016", "2016", "2016", "2016", "2016", 
    "2016", "2016", "2016", "2016", "2016", "2016", "2016", "2016", 
    "2016", "2016", "2016", "2016", "2016", "2016", "2016", "2016", 
    "2016", "2016", "2016"), Response = structure(c(1L, 2L, 3L, 4L, 
    5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
    19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
    9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
    22L, 23L, 24L), .Label = c("Trustworthy", "Supportive", "Leading", 
    "Strong", "Dependable", "Consultative", "Knowledgeable", "Sensible", 
    "Intelligent", "Consistent", "Stable", "Innovative", "Aggressive", 
    "Conservative", "Visionary", "Arrogant", "Professional", "Responsive", 
    "Confident", "Accessible", "Timely", "Focused", "Niche", "None"
    ), class = "factor"), Proportion = c(0.54, 0.48, 0.33, 0.35, 
    0.47, 0.3, 0.43, 0.29, 0.36, 0.38, 0.45, 0.32, 0.27, 0.22, 0.26, 
    0.95, 0.57, 0.42, 0.38, 0.5, 0.31, 0.31, 0.12, 0.88, 0.55, 0.55, 
    0.31, 0.4, 0.5, 0.34, 0.53, 0.3, 0.41, 0.41, 0.46, 0.34, 0.22, 
    0.17, 0.28, 0.94, 0.62, 0.46, 0.41, 0.53, 0.34, 0.36, 0.1, 0.84
    ), n = c(240L, 240L, 240L, 240L, 240L, 240L, 240L, 240L, 240L, 
    240L, 240L, 240L, 240L, 240L, 240L, 240L, 240L, 240L, 240L, 240L, 
    240L, 240L, 240L, 240L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 
    258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 258L, 
    258L, 258L, 258L, 258L, 258L, 258L)), .Names = c("Year", "Response", 
    "Proportion", "n"), row.names = c(NA, -48L), class = c("tbl_df", 
    "tbl", "data.frame"))
    

    Create a circular mapping on a scatterplot, using basics:

    df$degree <- seq(0,345,15) # 24 responses, equals 15 degrees per response
    df$o <- df$Proportion * sin(df$degree * pi / 180) # SOH
    df$a <- df$Proportion * cos(df$degree * pi / 180) # CAH
    df$o100 <- 1 * sin(df$degree * pi / 180) # Outer ring x
    df$a100 <- 1 * cos(df$degree * pi / 180) # Outer ring y 
    df$a75 <- 0.75 * cos(df$degree * pi / 180) # 75% ring y
    df$o75 <- 0.75 * sin(df$degree * pi / 180) # 75% ring x
    df$o50 <- 0.5 * sin(df$degree * pi / 180) # 50% ring x
    df$a50 <- 0.5 * cos(df$degree * pi / 180) # 50% ring y
    

    And plot. I cheated here to get them to connect in the last position by double plotting row 1 and 25 again:

    p = plot_ly()
    
    for(i in 1:24) {
      p <- add_trace(
        p, 
        x = c(d$o100[i],0), 
        y = c(d$a100[i],0), 
        evaluate = TRUE,
        line = list(color = "#d3d3d3", dash = "3px"),
        showlegend = FALSE
        )
    }
    
    p %>% 
      add_trace(data = d[c(1:48,1,25),], x = o, y = a, color = Year, 
                mode = "lines+markers",
                hoverinfo = "text", 
                text = paste(Year, Response,round(Proportion * 100), "%")) %>% 
      add_trace(data = d, x = o100, y = a100, 
                text = Response,
                hoverinfo = "none",
                textposition = "top middle", mode = "lines+text", 
                line = list(color = "#d3d3d3", dash = "3px", shape = "spline"),
                showlegend = FALSE) %>% 
      add_trace(data = d, x = o50, y = a50, mode = "lines", 
                line = list(color = "#d3d3d3", dash = "3px", shape = "spline"), 
                hoverinfo = "none",
                showlegend = FALSE) %>% 
      add_trace(data = d, x = o75, y = a75, mode = "lines", 
                line = list(color = "#d3d3d3", dash = "3px", shape = "spline"), 
                hoverinfo = "none",
                showlegend = FALSE) %>%
      layout(
        autosize = FALSE,
        hovermode = "closest",     
        autoscale = TRUE,
        width = 800,
        height = 800,
        xaxis = list(range = c(-1.25,1.25), showticklabels = FALSE, zeroline = FALSE, showgrid = FALSE),
        yaxis = list(range = c(-1.25,1.25), showticklabels = FALSE, zeroline = FALSE, showgrid = FALSE))
    

    As you can see, I've got it with the exception of that last connecting line, and the lines that pass from the origin to the text of the response.

提交回复
热议问题