[英]R geom_area and coord_polar conversion to plotly
I am trying to convert a ggplot polar chart to a plotly one. 我正在尝试将ggplot极坐标图转换为可绘制的极坐标图。 I am missing the cumulative of
geom_area
in plotly and the filled color for each trace that is not going to the origin for both trace. 我在
geom_area
中缺少了geom_area
的累积值, geom_area
缺少了geom_area
迹线都不会到达原点的每个迹线的填充颜色。 The goal is to be as closed as possible to the ggplot view. 目的是尽可能地关闭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
Thank you for any clue on how to do this. 感谢您提供有关执行此操作的任何线索。
Here is a starting point for the solution of your problem: 这是您解决问题的起点:
k <- 100
df <- data.frame(angle = rep(seq(0, 360, length.out=k), 4),
frequency = rep(1:k, 4),
z = as.character(c(rep(1, k), rep(2, k))))
df <- aggregate(frequency ~ angle + z, data = df, sum)
df$frequency[df$z==2] <- df$frequency[df$z==2]/2
p <- plot_ly(data=df, type = 'scatterpolar', mode = "lines") %>%
add_trace(theta=~angle,r=~frequency,fill="toself", color=~z)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.