ggrgl:用ggplot做3D图表

写在前面

rgl包我们知道在R语言里面做3D图表很好,这里是之前r语言中一些3D图表教程,点击。或者转化ggplot为3D图形,点击查看,在今年11月23日,cran跟新的ggrgl包,用于ggplot的3D图形的绘制,使用了图形语法,更加方便的做3D图形。

ggrgl依赖很多R包,这里一次性安装。

新的标度 Z

z标尺和x或者y标尺一样的使用方法,z坐标轴通过设置extrude = TRUE(在支持它的geoms上),凸起的元素与地面相连,就好像它是从地面上被挤压出来的一样。

有新的z标尺,就有新的设置scale函数:

- extrude_z Lower limit of extrusion 0.05

- extrude_face_fill Extruded face colour grey20

- extrude_face_alpha Extruded face alpha 1

- extrude_edge_colour Edge colour for extrusion NA

- extrude_edge_alpha Edge alpha for extrusion 1

- extrude_edge_size Width of line for extruded edges 1

三维图层两种类型 z和3D

library(ggplot2)
library(ggrgl)
p <- ggplot(mpg) +
geom_bar_z(aes(x=class, fill=class), colour='black', z=200, extrude=TRUE)

devoutrgl::rgldev()
p

invisible(dev.off())

# ?geom_line_3d
p <- ggplot(mpg) +
geom_line_3d(aes(x=displ, y = cty,color=class), colour='black', z=200, extrude=TRUE)
devoutrgl::rgldev()
p

set.seed(1)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Model Parameters
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
N <- 20
zoffset <- 10
theta_inc <- 10
helix_r <- 1
theta <- seq_len(N) * theta_inc * pi/180

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create model
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dna <- data.frame(
x1 = helix_r * cos(theta),
y1 = helix_r * sin(theta),
x2 = helix_r * cos(theta + pi),
y2 = helix_r * sin(theta + pi),
z = seq_len(N) * zoffset,
base = sample(c('A', 'T', 'C', 'G'), size = N, replace = TRUE)
)

dna$cbase <- c(A='T', T='A', C='G', G='C')[dna$base]

p <- ggplot(dna) +
geom_sphere_3d(aes(x1, y1, z = z, colour = base), size = 15) +
geom_sphere_3d(aes(x2, y2, z = z, colour = cbase), size = 15) +
geom_segment_3d(aes(x = x1, y = y1, z = z, xend = x2, yend = y2, zend = z), alpha = 0.25) +
coord_equal() +
theme_ggrgl() +
labs(
title = "Simple DNA Model",
subtitle = "ggrgl::geom_sphere_3d() + geom_segment_3d() with {devoutrgl}"
) +
scale_color_brewer(palette = 'Dark2')

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# plot in 3d with devoutrgl
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev(fov = 30, view_angle = -40, zscale = 5)
p
invisible(dev.off())

p <- ggplot(mpg) +
geom_bar_z(
aes(x = class, fill = class, extrude_face_fill = class),
colour = 'black',
z = 50,
extrude = TRUE,
extrude_edge_colour = 'grey10'
) +
labs(
title = "ggrgl::geom_bar_z()",
subtitle = "with {devoutrgl}"
) +
theme_ggrgl()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render Plot in 3d with {devoutrgl}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev(fov = 30, view_angle = -30)
p
invisible(dev.off())

plot_df <- data.frame(
group = factor(c("Cool", "But", "Use", "Less"),
levels = c("Cool", "But", "Use", "Less")),
value = c(20, 20, 30, 30),
z = c(10, 30, 5, 15)
)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Use `geom_bar_z()`
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p <- ggplot(plot_df, aes(x="", y=value, fill=group, z = z)) +
geom_bar_z(width = 1, stat = "identity", extrude = TRUE, extrude_face_fill = 'grey40') +
coord_polar("y", start=0) +
theme_ggrgl() +
labs(
title = "ggrgl::geom_bar_z()",
subtitle = "with {devoutrgl}"
)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render Plot in 3d with {devoutrgl}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev(fov = 30, view_angle = -30)
p
invisible(dev.off())

library(CP1919) # Pulsar data used for Joy Division album cover
library(ggplot2)
library(dplyr)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Tweak the pulsar data such that the baseline is always at y = 0
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pulsar_df <- CP1919 %>%
group_by(line) %>%
mutate(
y = y - min(y)
) %>%
ungroup()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ribbon plot
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p <- ggplot(pulsar_df) +
geom_ribbon_z(aes(x, ymax=y, group=line, z = line), ymin = 0, colour='white') +
theme_void() +
coord_fixed()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render Plot in 3d with {devoutrgl}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev(fov = 30, view = 'flat', view_flat_angle = 30, zscale = 4)
p
invisible(dev.off())
rgl::rgl.bg(color = 'black')

huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron))

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create plot with `geom_ribbon_z()`
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p <- ggplot(huron, aes(year)) +
geom_ribbon_z(aes(ymin=level-1, ymax=level+1), z = 100, extrude = TRUE,
extrude_face_fill = 'grey50', keep2d = TRUE) +
labs(
title = "ggrgl::geom_ribbon_z()",
subtitle = "with {devoutrgl}"
)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render Plot in 3d with {devoutrgl}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev(fov = 30, view_angle = -30)
p
invisible(dev.off())

set.seed(1)
N <- 10
x <- LETTERS[1:N]
y <- paste0("var", seq(1,N))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(N*N, 0, 5)

p <- ggplot(data, aes(X, Y, fill= Z, z= Z)) +
geom_tile_z(extrude = TRUE, mapping = aes(extrude_face_fill = Z)) +
coord_equal() +
labs(
title = "ggrgl::geom_tile_z() rendering heights",
subtitle = "with {devoutrgl}"
)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render Plot in 3d with {devoutrgl}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev(fov = 30, view_angle = -30)
p
invisible(dev.off())

library(dplyr)
library(ggplot2)
library(purrr)
library(tibble)
library(tidyr)
library(rgl)
library(ggrgl)
library(viridis)
p <- volcano %>%

# Data wrangling
as_tibble() %>%
rowid_to_column(var="X") %>%
gather(key="Y", value="Z", -1) %>%

# Change Y to numeric
mutate(Y=as.numeric(gsub("V","",Y))) %>%

# Viz
ggplot(aes(X, Y, fill= Z, colour = Z, z = Z)) +
geom_tile_z(extrude = TRUE) +
theme_ggrgl() +
theme(legend.position="none") +
scale_fill_viridis_c(option = 'A') +
scale_colour_viridis_c(option = 'A') +
coord_equal() +
labs(
title = "ggrgl::geom_tile_z() rendering volcano",
subtitle = "with {devoutrgl}"
)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render Plot in 3d with {devoutrgl}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev(fov = 30, view_angle = -30)
p
invisible(dev.off())

library(ggplot2)
library(rgl)
library(ggrgl)
library(ambient)
library(dplyr)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set.seed(3)
N <- 30

dat <- long_grid(x = seq(0, 10, length.out = N), y = seq(0, 10, length.out = N)) %>%
mutate(
noise =
gen_perlin(x, y, frequency = 0.3) +
gen_perlin(x, y, frequency = 2) / 10
)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Plot each location as a coloured tile
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(dat) +
geom_tile(aes(x, y, fill = noise)) +
scale_fill_gradientn(colours = topo.colors(10)) +
theme_bw() +
coord_equal()

p <- ggplot(dat, aes(x, y, z = noise)) +
geom_tile_z(aes(fill = noise), colour = NA) +
labs(
title = "ggrgl::geom_tile_z()",
subtitle = "with {devoutrgl}"
) +
theme_ggrgl() +
scale_fill_gradientn(colours = topo.colors(10)) +
coord_equal() +
theme(legend.position = 'none')

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render Plot in 3d with {devoutrgl}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev(fov = 30, view_angle = -30)
p
invisible(dev.off())

(0)

相关推荐

  • 在x方向上扩展ggplot`geom_ribbon()

    https://stackoverflow.com/questions/55290819/extend-ggplot-geom-ribbon-in-the-x-directionlibrary(tid ...

  • ggplot2作图小例子

    ggplot2绘制几个常用图形:直方图,密度曲线图,散点图,箱线图,小提琴图,折线图(重点在小提琴图) ggplot2直方图 library(ggplot2) ggplot(data = diamon ...

  • 这个豪华酷炫的3D图表,你也能轻松做出来

    这个豪华酷炫的3D图表,你也能轻松做出来

  • 做个图表很简单 - 专业的簇状柱形图

    今天为大家介绍如何制作专业的簇状柱形图.(文末有视频教程) 如果我们需要为下面的数据设计可视化图表: 最简单也是最常用的图表就是簇状柱形图: 但是这个图表显得不够专业.我们可以通过下面的步骤来制作专业 ...

  • PS怎么做3D立体字效果

    搜狗指南  游戏/数码>互联网

  • 不会吧不会吧!不会还有人不会做可视化图表吧?

    相较于传统的图表与文档,可视化能够将数据通过更加直观的方式展示出来,在日常生活中,可视化图表的应用其实已经很常见了,比如企业的工作总结汇报.比如方案的策划与制定.再比如数据的归纳与整理等等.大部分人可 ...

  • Rotato:做3D样机,这款软件简单到爆

    前一段时间,有一个新的工具发布-Rotato,用它可以快速制作3D样机,就是封面的视觉效果.很早就知道了这款软件,但一直没用,尝试了一下发现:真的太强大了! 01.什么是样机 如果用一个词来解释[样机 ...

  • 办公小技巧:利用文本框巧做创意图表

    说起文本框,也许一般人会认为它只是用来插入文字,再简单不过了.其实,这样的认识未免有些肤浅,如果把文本框潜在的能力挖掘出来,定会让幻灯片大放异彩.接下来,我们就一同来学习,如何利用文本框制作创意图表, ...

  • 你如果会用Excel做动态图表,又何必加班到深夜?

    动态图表是一种根据我们的选择,来实时展示不同信息的图表. 它可以让数据由静态转为动态,更生动,更有灵魂地表现出来. 比如在一个图表内,可以动态展示各种商品在不同季度的销售情况: 以便知道不同季节的畅销 ...

  • 你以为Excel只能做数据图表?有试过作图吗?

    本期技巧共 605 字,预计阅读时间 6 分钟 高手图片处理用PS.AI,我们用PPT,然而我们用PPT,他却用Excel,极限画家堀内辰男,这是日本一位用Excel绘图的老爷爷,60岁退休才开始学E ...

  • 有它做3D直播,papi酱可能会怕我

    文/菱角 直播以及自媒体视频,当下中国网络最火之一,网红们日进斗金. 然而,业者骤增.政策陡变,蓝海成红海,网红们日子不好过.papi 酱也逃不出这个套. 这个时候考验创意! 而这个未来 3D 摄影机 ...