【问题标题】:R: possible to make a radar chart, but 3d surface version with ggplot or plotly?R:可以制作雷达图,但是 ggplot 或 plotly 的 3d 表面版本?
【发布时间】:2021-12-09 20:28:40
【问题描述】:

在网络上的各种 R 图表库中找不到任何关于此的指导。

我有这样的数据:

YearMonth Product Sales
202101 bike 100
202101 car 40
202101 skateboard 60
202102 bike 70
202102 car 30
202102 skateboard 50
202103 bike 50
202103 car 20
202103 skateboard 30

我希望看到的是连接 3 个雷达图(每个月一个)的表面。对于这个例子来说,它会有点锥形,因为每个产品的销售额都会随着时间的推移而下降。

在 R 中使用 ggplot 或 plotly 可以实现这样的事情吗?

【问题讨论】:

  • ggplot2 否。 ggplot 不做 3D 图。还有一些其他软件包,例如“rgl”,更适合 3D。我不建议这样做,因为我认为 3D 雷达图过于复杂,读者无法快速理解。
  • 这当然是可能的,因为最终您可以计算出此类对象的顶点并以多种方式之一绘制生成的网格。但是,我不知道有任何软件包可以轻松完成此类事情。另外,我同意@Dave2e - 尽管它看起来很酷,但它肯定会是一个非常无信息的图形。你有这样一个情节的链接,让我们知道你在找什么吗?

标签: r ggplot2 plotly ggplotly


【解决方案1】:

我倾向于同意 Dave2e ,但我在尝试编写代码时学到了一些东西。
即使是 ggplot2 中的基本雷达图也不明显。
首先,我必须重复自行车数据,以便 geom_path 知道关闭多边形。
然后我不得不稍微修改coord_polar() 以强制使用直线。
然后我使用rayshader 包将其制作为 3D。 这里有一个问题是您需要使用 guides() 函数(而不是 scale_color_continuous(guide = "none") 来关闭指南。

library(ggplot2)
library(rayshader)

df <- data.frame(
  YearMonth = c(202101L,202101L,202101L,
                202102L,202102L,202102L,202103L,202103L,202103L),
  Product = c("bike","car","skateboard",
              "bike","car","skateboard","bike","car","skateboard"),
  Sales = c(100L, 40L, 60L, 70L, 30L, 50L, 50L, 20L, 30L)
)
df <- rbind(df, subset(df, subset = Product == "bike"))
df$height <- match(df$YearMonth, sort(unique(df$YearMonth)))
df

# Define a new coordinate system from coord_polar
coord_radar <- function(theta = "x", start = 0, direction = 1, clip = "on") {
  theta <- match.arg(theta, c("x", "y"))
  r <- if (theta == "x")
    "y"
  else "x"
  ggproto(NULL, CoordPolar, theta = theta, r = r, start = start,
          direction = sign(direction), clip = clip,
          # This is the change to make the lines straight
          is_linear = function() TRUE
          )
}

plot2d <- ggplot(df, aes(x = Product, y = Sales, color = height)) + 
  geom_path(aes(group = YearMonth)) + 
  scale_color_continuous() +
  guides(color = "none") + 
  coord_radar()

plot_gg(plot2d, raytrace = FALSE)

给:

【讨论】:

  • 感谢您的努力! FTR 我倾向于同意你们三个的观点——只是想想出一些很酷的方式来展示销售类别随时间的演变——一个半透明的“管”沿其垂直轴扩张/收缩看起来可能很酷.我当然可以简单地做一个多面图表,无论是雷达还是其他。 :)
猜你喜欢
  • 2021-02-02
  • 1970-01-01
  • 2017-02-08
  • 2018-05-06
  • 2020-08-13
  • 2018-06-24
  • 2021-05-02
  • 2018-08-04
  • 2017-07-09
相关资源
最近更新 更多