比如,用iris数据做例子:
- library(scatterplot3d)
- # set color
- col_blue <- hsv(h = 0.66, s = (iris$Petal.Width - min(iris$Petal.Width))/(diff(range(iris$Petal.Width))) * 0.6 + 0.4 , v = 1)
- # Plot
- scatterplot3d(iris[,1:3], pch = 16, color = col_blue)
现在介绍另外一个图形,以另外一种平面方式展示三维以上的连续数据——ggtern包
- # cran上有这个包,但版本是2.2.1,本帖中要用到2.2.2,因此要用到另外一个安装方法
- devtools::install_git('https://bitbucket.org/nicholasehamilton/ggtern')
- library(tidyverse)
- library(ggtern)
- # 以下作图
- iris %>%
- ggtern(aes(Sepal.Length, Sepal.Width, Petal.Length)) +
- geom_hex_tern(bins = 5, aes(value = Petal.Width, fill = ..count..))
有点意思了吧,还能信息量还能更加丰富一些:
- iris %>%
- ggtern(aes(Sepal.Length, Sepal.Width, Petal.Length)) +
- theme_bw() +
- geom_hex_tern(bins = 5,fun = mean, aes(value = Petal.Width, fill = ..stat..)) +
- stat_hex_tern(bins = 5,fun = mean,
- geom = 'text',
- aes(value = Petal.Width, label = sprintf("%.2f", ..stat..)),
- size = 3, color = 'white')
除了六边形式,还能以三角形式展示:
- iris %>%
- ggtern(aes(Sepal.Length, Sepal.Width, Petal.Length)) +
- theme_bw() +
- geom_tri_tern(bins = 5, fun = mean,
- aes(value = Petal.Width, fill = ..stat..)) +
- stat_tri_tern(bins = 5, fun = mean,
- geom = 'text',
- aes(value = Petal.Width, label = sprintf("%.2f", ..stat..)),
- size = 3, color = 'white', centroid = TRUE)