我可以在情节中重新创建这个极坐标蜘蛛图吗? [英] Can I recreate this polar coordinate spider chart in plotly?

查看:16
本文介绍了我可以在情节中重新创建这个极坐标蜘蛛图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何使用 plotly 重新创建蜘蛛(或雷达)图的以下图形时遇到了一些困难.实际上,我什至无法在最新版本的 ggplot2 中重新创建它,因为自 1.0.1 以来发生了重大变化.

这是一个示例图形:

这是构建它的原始函数:

plotly 4.x 版更新

更新中的重大更改意味着如果不进行一些修改以使其保持最新状态,原始版本将不再有效.这是一个更新的版本:

库(data.table)gridlines1 = data.table(lat = -90 + scale*(c(0.1, 0.25, 0.5, 0.75, 1)))gridlines1 = gridlines1[, .(long = c(seq(0,360, length.out=100), NA)), by = lat]gridlines1[is.na(long), lat := NA]gridlines2 = data.table(long = seq(0,360, length.out=25)[-1])gridlines2 = gridlines2[, .(lat = c(NA, -90, -90+scale, NA)), by = long]gridlines2[is.na(lat), long := NA]text.labels = data.table(lat=seq(-90, -90+scale,length.out = 5),长 = 0,text=c("、25%"、50%"、75%"、100%"))p = plot_ly() %>%add_trace(type="scattergeo", data = d[c(1:24, 1, 25:48, 25),],纬度=~纬度,经度=~长,颜色 = 因子(d[c(1:24, 1, 25:48, 25),]$Year),模式='线+标记')%>%布局(地理=列表(范围='世界',showland=F,showcoastlines=F,showframe=F,投影=列表(类型='方位角等距',旋转=列表(纬度=-90),比例= 5)),图例 = 列表(x=0.7, y=0.85)) %>%add_trace(data = gridlines1, lat=~lat, lon=~long,类型='scattergeo',模式='线',线=l1,showlegend=F,继承=F)%>%add_trace(data = gridlines2, lat=~lat, lon=~long,type='scattergeo', mode='lines', line=l2, showlegend=F) %>%add_trace(data = text.labels, lat=~lat, lon=~long,类型=scattergeo",模式=文本",文本=〜文本,文本字体=列表(大小= 12),showlegend=F,继承=F)%>%add_trace(data = d, lat=-90+scale*1.2, lon=~long,类型=scattergeo",模式=文本",文本=〜响应,文本字体=列表(大小= 10),showlegend=F,继承=F)p

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 recent versions of ggplot2 because there have been breaking changes since 1.0.1.

Here's an example graphic:

Here's the original function that built it:

http://pcwww.liv.ac.uk/~william/Geodemographic%20Classifiability/func%20CreateRadialPlot.r

Here's an example of how the original function worked:

http://rstudio-pubs-static.s3.amazonaws.com/5795_e6e6411731bb4f1b9cc7eb49499c2082.html

Here's some not so dummy data:

d <- structure(list(Year = rep(c("2015","2016"),each=24),
                    Response = structure(rep(1L:24L,2), 
                                         .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 = rep(c(240L,258L),each=24)),
               .Names = c("Year", "Response", "Proportion", "n"), 
               row.names = c(NA, -48L), class = c("tbl_df", "tbl", "data.frame"))

Here's my attempt (not very good)

plot_ly(d, r = Proportion, t = Response, x = Response, 
        color = factor(Year), mode = "markers") %>%
layout(margin = list(l=50,r=0,b=0,t=0,pad = 4), showlegend = TRUE)

Any thoughts on how I might be able to recreate this using plotly?

解决方案

The options available with polar plots are still limited. There is not, so far as I can tell, any way to add text to a polar plot for the category labels around the circumference. Neither text scatter points, nor annotations nor tick labels (except at the four quarter points) are compatible with polar coordinates in plotly at the moment.

So, we need to get a little creative.

One type of polar coordinate system that does work nicely is a projected map of a sperical earth using an azimuthal projection. Here is a demonstration of how you might adapt that to this problem.

First, convert the values to plot into latitude and longitudes centred on the South pole:

scale <- 10   # multiply latitudes by a factor of 10 to scale plot to good size in initial view
d$lat <- scale*d$Proportion - 90
d$long <- (as.numeric(d$Response)-1) * 360/24

Plot using an azimuthal equidistant projection

p <- plot_ly(d[c(1:24,1,25:48,25),], lat=lat, lon=long, color = factor(Year), colors=c('#F8756B','#00BDC2'),
             type = 'scattergeo', mode = 'lines+markers', showlegend=T) %>%
layout(geo = list(scope='world', showland=F, showcoastlines=F, showframe=F,
             projection = list(type = 'azimuthal equidistant', rotation=list(lat=-90), scale=5)), 
             legend=list(x=0.7,y=0.85))

Put some labels on

p %<>% add_trace(type="scattergeo",  mode = "text", lat=rep(scale*1.1-90,24), lon=long, 
                 text=Response, showlegend=F, textfont=list(size=10)) %>%
       add_trace(type="scattergeo",  mode = "text", showlegend=F, textfont=list(size=12),
                 lat=seq(-90, -90+scale,length.out = 5), lon=rep(0,5), 
                 text=c("","25%","50%","75%","100%"))

Finally, add grid lines

l1 <- list(width = 0.5, color = rgb(.5,.5,.5), dash = "3px")
l2 <- list(width = 0.5, color = rgb(.5,.5,.5))
for (i in c(0.1, 0.25, 0.5, 0.75, 1)) 
    p <- add_trace(lat=rep(-90, 100)-scale*i, lon=seq(0,360, length.out=100), type='scattergeo', mode='lines', line=l1, showlegend=F, evaluate=T)
for (i in 1:24) 
    p <- add_trace(p,lat=c(-90+scale*0.1,-90+scale), lon=rep(i*360/24,2), type='scattergeo', mode='lines', line=l2, showlegend=F, evaluate=T)

Update for plotly version 4.x

Breaking changes in the updates to plotly mean that the original version no longer works without a few modifications to bring it up to date. here is an updated version:

library(data.table)
gridlines1 = data.table(lat = -90 + scale*(c(0.1, 0.25, 0.5, 0.75, 1)))
gridlines1 = gridlines1[, .(long = c(seq(0,360, length.out=100), NA)), by = lat]
gridlines1[is.na(long), lat := NA]

gridlines2 = data.table(long = seq(0,360, length.out=25)[-1])
gridlines2 = gridlines2[, .(lat = c(NA, -90, -90+scale, NA)), by = long]
gridlines2[is.na(lat), long := NA]

text.labels = data.table(
  lat=seq(-90, -90+scale,length.out = 5),
  long = 0,
  text=c("","25%","50%","75%","100%"))

p = plot_ly() %>%
add_trace(type="scattergeo", data = d[c(1:24, 1, 25:48, 25),], 
      lat=~lat, lon=~long, 
      color = factor(d[c(1:24, 1, 25:48, 25),]$Year), 
      mode = 'lines+markers')%>%
layout(geo = list(scope='world', showland=F, showcoastlines=F, showframe=F,
    projection = list(type = 'azimuthal equidistant', rotation=list(lat=-90), scale=5)), 
    legend = list(x=0.7, y=0.85)) %>%
add_trace(data = gridlines1, lat=~lat, lon=~long, 
    type='scattergeo', mode='lines', line=l1, 
    showlegend=F, inherit = F)  %>%
add_trace(data = gridlines2, lat=~lat, lon=~long,
    type='scattergeo', mode='lines', line=l2, showlegend=F) %>%
add_trace(data = text.labels, lat=~lat, lon=~long, 
  type="scattergeo", mode = "text", text=~text, textfont = list(size = 12),
    showlegend=F, inherit=F) %>%
add_trace(data = d, lat=-90+scale*1.2, lon=~long, 
    type="scattergeo", mode = "text", text=~Response, textfont = list(size = 10),
    showlegend=F, inherit=F) 

p

这篇关于我可以在情节中重新创建这个极坐标蜘蛛图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆