【发布时间】:2017-09-09 10:25:44
【问题描述】:
我在弄清楚如何使用 plotly 重新创建蜘蛛(或雷达)图的以下图形时遇到了一些困难。实际上,我什至无法在最新版本的 ggplot2 中重新创建它,因为自 1.0.1 以来发生了重大变化。
这是一个示例图形:
这是构建它的原始函数:
http://pcwww.liv.ac.uk/~william/Geodemographic%20Classifiability/func%20CreateRadialPlot.r
以下是原始函数如何工作的示例:
http://rstudio-pubs-static.s3.amazonaws.com/5795_e6e6411731bb4f1b9cc7eb49499c2082.html
这里有一些不那么虚拟的数据:
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"))
这是我的尝试(不是很好)
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)
关于如何使用 plotly 重新创建它有什么想法吗?
【问题讨论】:
-
我很想知道为什么最近的
ggplot2版本会破坏您现有的代码。或者,反过来说,你为什么要求plotly解决方案而不是修复现有的ggplot2代码? -
@UweBlock 它基于我不太了解的代码,这可能需要大量时间来调试和修复。我第一次尝试 plotly 给了我一个非常接近的解决方案。所以我想有更多经验的人会看到这个情节,也许知道怎么做。我已经包含了该功能以及如何在上述问题的更新中使用它的示例。欢迎您尝试修复 ggplot 代码。
-
您是否尝试将 mode="markers" 替换为 mode="lines"?
-
有一种简单的方法可以使用
ggplotly(yourplot)plot.ly/ggplot2/getting-started 将ggplot2绘图转换为plotly。但是,我无法使它与这种情节一起使用。这是您的数据ggplot(d, aes(y = Proportion, x = Response, group = factor(Year), colour = factor(Year))) + coord_polar() + geom_point() + geom_path()的静态ggplot2图 -
@MLavoie 是的,但是它的排列方式有问题,我不知道如何将每个属性的“xaxis”标签放在圆圈周围。