我知道这肯定是基本的事情,但是很难找到问题所在。 这是我简单的可复制的R代码:
library(plotly)
risk_status <- data.frame(
id <- c(1:6),
r <- c(1, 2, 3, 4, 3, 2),
theta <- c(30, 90, 180, 220, 270, 330),
bubbleSize <- c(10, 20, 30, 40, 50, 60),
name <- c("Rick","Dan","Michelle","Ryan","Gary", "Pedro"),
status <- c("Low", "Medium", "Low", "Medium", "Low", "Low")
)
risk_status$status <- factor(risk_status$status, levels = c("Low", "Medium", "High"))
bubble_colors_vector <- c("red", "green", "blue")
p <- plot_ly( data = risk_register_data) %>%
add_trace(
type = 'scatterpolar',
r = ~r,
theta = ~theta,
size = as.numeric(risk_status$status),
sizes = c(100,500)#,
#color = risk_status$status,
#colors = bubble_colors_vector
) %>%
add_trace(
type = "scatterpolar",
mode = "text",
r = risk_status$r,
theta = risk_status$theta,
text = risk_status$id
)
p
图表输出:
到目前为止,很好。
但是现在让我们说,如果我想在其中引入颜色,只需取消注释color
和colors
参数即可。
我得到这张图:
假设我使用color = as.numeric(risk_status$status)
在那种情况下,我得到这样的图表
在这里,我不要这样的规模和传说。
我知道,这是有问题的,因为尽管factoring
数据帧的status
列中,我提供了"Low", "Medium", "High"
作为级别,而在列中仅存在"Low", "Medium"
。
但是,这也是正常情况,如果没有颜色,气泡的大小会正确显示,那么为什么在添加颜色时也会缺少大小呢?
预先感谢!
答案 0 :(得分:0)
最后,
我通过在add_trace
中使用for loop
的解决方法解决了该问题。
以下是代码:
library(plotly)
risk_status <- data.frame(
id <- c(1:6),
r <- c(1, 2, 3, 4, 3, 2),
theta <- c(30, 90, 180, 220, 270, 330),
bubbleSize <- c(10, 20, 30, 40, 50, 60),
name <- c("Rick","Dan","Michelle","Ryan","Gary", "Pedro"),
status <- c("Low", "Medium", "Low", "Medium", "Low", "Low")
)
risk_status$status <- factor(risk_status$status, levels = c("Low", "Medium", "High"))
bubble_colors_vector <- c("red", "green", "blue")
p <- plot_ly( data = risk_register_data) %>%
add_trace(
type = 'scatterpolar',
r = risk_status$r[risk_status$status == 'Low'],
theta = risk_status$theta[risk_status$status == 'Low'],
#size = as.numeric(risk_status$status[risk_status$status == 'Low']),
#sizes = c(100,500),
#color = 'red', #risk_status$status[risk_status$status == 'Low'], #as.numeric(risk_status$status), #risk_status$status,
#colors = bubble_colors_vector[risk_status$status == 'Low'],
mode = 'markers',
marker = list(symbol = 'circle', line = list(width = 1, color = 'black'), mode="scatterpolar", color = "red", size=20)
) %>%
add_trace(
type = 'scatterpolar',
r = risk_status$r[risk_status$status == 'Medium'],
theta = risk_status$theta[risk_status$status == 'Medium'],
#size = as.numeric(risk_status$status[risk_status$status == 'Medium']),
#sizes = c(100,500),
#color = 'red', #risk_status$status[risk_status$status == 'Medium'], #as.numeric(risk_status$status), #risk_status$status,
#colors = bubble_colors_vector[risk_status$status == 'Medium'],
mode = 'markers',
marker = list(symbol = 'circle', line = list(width = 1, color = 'black'), mode="scatterpolar", color = "yellow", size=20)
) %>%
add_trace(
type = 'scatterpolar',
r = risk_status$r[risk_status$status == 'High'],
theta = risk_status$theta[risk_status$status == 'High'],
#size = as.numeric(risk_status$status[risk_status$status == 'High']),
#sizes = c(100,500),
#color = 'red', #risk_status$status[risk_status$status == 'High'], #as.numeric(risk_status$status), #risk_status$status,
#colors = bubble_colors_vector[risk_status$status == 'High'],
mode = 'markers',
marker = list(symbol = 'circle', line = list(width = 1, color = 'black'), mode="scatterpolar", color = "green", size=20)
) %>%
add_trace(
type = "scatterpolar",
mode = "text",
r = risk_status$r,
theta = risk_status$theta,
text = risk_status$id
)
p