首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在具有多个z轴的3D中绘制“ggridges”类型的图

如何在具有多个z轴的3D中绘制“ggridges”类型的图
EN

Stack Overflow用户
提问于 2021-04-24 21:08:31
回答 2查看 120关注 0票数 1

我正在寻找一个在ggplot中的包来制作下面的图。

我知道ggridges,但它没有3个维度。

EN

回答 2

Stack Overflow用户

发布于 2021-04-25 00:58:56

您还可以使用ggridges欺骗x轴和网格线以获得相同的效果:

代码语言:javascript
复制
library(ggridges)
library(ggplot2)

slant_factor = 5   # How many units x shift per category? Won't work with zero

intercepts = (20 * (0:4)) / slant_factor + 0.8
ggplot(lincoln_weather, 
       aes(x = `Mean Temperature [F]` - as.numeric(Month)*slant_factor, 
           y = Month, fill = stat(x))) +
  
  geom_abline(slope = -1 / slant_factor , 
              intercept = intercepts,
              color = "gray90") +
  
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01, gradient_lwd = 1.) +
  
  scale_x_continuous(expand = c(0, 0),
                     breaks = 20 * (0:4)) +
  scale_y_discrete(expand = expand_scale(mult = c(0.01, 0.25))) +
  scale_fill_viridis_c(name = "Temp. [F]", option = "C") +
  labs(
    title = 'Temperatures in Lincoln NE',
    subtitle = 'Mean temperatures (Fahrenheit) by month for 2016'
  ) +
  theme_ridges(font_size = 13, grid = TRUE) + 
  theme(axis.title.y = element_blank(), panel.grid.major.x = element_blank())

票数 1
EN

Stack Overflow用户

发布于 2021-04-24 23:13:09

解决您的问题的两个可能的选项(如果我理解正确的话)如下:

代码语言:javascript
复制
# dummy data
df <- data.frame(grps = factor(sort(rep(c("a","b","c"), 10))),
                 seq = rep(1:10, 3),
                 vlr = c(c(1:6, 4:1), c(1:6, 4:1) + 1, c(1:6, 4:1) + 2))

使用plotly:

代码语言:javascript
复制
library(plotly)

fig <- plot_ly(df, x = ~vlr, y = ~seq, z = ~grps, type = 'scatter3d', mode = 'lines',
  transforms = list(
    list(
      type = 'groupby',
      groups = df$grps)))

fig # you have to rotate it

使用rayshader将线状图转换为3D,但它将转换为条形图(它的工作诀窍是:定义高度的值进入颜色美感)

代码语言:javascript
复制
library(ggplot2)
library(rayshader)

gl <- ggplot2::ggplot(df, aes(x = grps, y = seq, group = grps, color = vlr)) + 
    ggplot2::geom_line()

rayshader::plot_gg(gl)

在使用geom_ridgeline()并通知height参数时,ggridges可以处理第三维,但它似乎有点有限(尽管我没有阅读文档以及如何润色和改进这种类型的图表:

代码语言:javascript
复制
library(ggplot2)
library(ggridges)

ggplot2::ggplot(df, aes(x = seq, height = vlr, y = grps, color = grps)) + 
  ggridges::geom_ridgeline()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67243131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档