我正在尝试将 ggplot 极坐标图转换为 plotly 极坐标图。我错过了geom_area
每条轨迹的累积和填充颜色,这两条轨迹都不会到达原点。目标是尽可能接近 ggplot 视图。
df <- data.frame(angle = rep(seq(0, 360, 45), 4), frequency = rep(1:9, 4), z = as.character(c(rep(1, 9), rep(2, 9))))
df <- aggregate(frequency ~ angle + z, data = df, sum)
pl <- ggplot(df,aes(x=angle,y=frequency,fill=z)) + geom_area() + coord_polar() + scale_x_continuous(breaks=angle, labels=c("N", "NE", "E", "SE", "S", "SO", "O", "NO", "N"))
pl
p <- plot_ly(type = 'scatterpolar', mode = "scatter")
for (g in 1:2){
p <- p %>%
add_trace(data = df[df$z == g,],
theta = ~df$angle, r = ~df$frequency,
opacity = 1, fill = "tozeroy")
}
p <- p %>% layout(
polar = list(radialaxis = list(angle = 90),
angularaxis = list(
rotation = 90,
direction = 'clockwise',
ticktext = c("N", "NE", "E", "SE", "S", "SO", "O", "NO", "N"),
tickvals = seq(0, 360, 45)
)
)
)
p
感谢您提供有关如何执行此操作的任何线索。