我的目标是使用plotly
制作风玫瑰图表,绘制5个变量(SG
)的国家/地区(X1, X2, X3, X4, X5
)评级。
可重现代码
library(plotly) # viz
library(tidyr) # data munge
library(dplyr) # data munge
# list of countries
Countries <- c("SG", "UK", "CAD", "USA", "AU")
# data
set.seed(1)
data.frame(replicate(5, sample(0:10, 5, rep=TRUE))) %>%
cbind(Countries) %>%
gather(key = Variable
, value = value
, -Countries) ->
df
# single country
SG <- df[df$Countries %in% c("SG"),]
# plot
plot_ly(SG
, r = value #radial
, t = Variable #angular coordinates
, color = Variable
, type = "area") %>%
layout(radialaxis = list(ticksuffix = "pts")
, orientation = 270
, autosize = T
, width = 500
, height = 500
, margin = list(l = 100
, r = 50
, b = 100
, t = 50
, pad = 0
, autoexpand=FALSE)) %>%
config(displayModeBar = F, showLink = F) ->
p
p
问题
更新
根据@ MLavoie的评论,需要注意的另一点是,当你绘制df
时,情节不是空白画布(见下面的截图2)。但是,它似乎并不反映输入数据。例如。图中的变量X1的值为8,而数据的值为sum(df[df$Variable == "X1", "value"])
= 23。