一、概述
《How effective is the health promotion policy in Sichuan, China: based on the PMC-Index model and field evaluation》
《Spectrum-effect relationship between UPLC fingerprints and anti-lung cancer effect of Panax ginseng》
很酷吧,好的图对文章也有加分的作用,接下来我们看看三维图是怎么做的吧?
二、数据集
任何图都离不开数据集,所以先看数据集吧。
2.1 安装及其使用
#相关R包安装与载入:
#install.packages('plotly')
library(plotly)
#install.packages("Seurat","cols4all")
library(Seurat)
library(cols4all)
#install.packages("ggfun") 在安装tidydr之前需要先安装依赖包ggfun才能安装成功
#install.packages("tidydr","dplyr","ggplot2")
library(tidydr)
library(dplyr)
library(ggplot2)
#install.packages("umap")
library(umap)
2.2 读取数据
首先,我们需要读取obj.Rda数据集,查看数据集信息
data(iris)
str(iris)
数据集展示:
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
三、基础用法
plotly是一个交互式高质量图表绘制R包,如折线图、散点图、面积图、柱形图、箱线图、直方图等等,当然也包括了3D型图表的绘制,如三维散点图、线图、曲面图等。
3.1 三位散点图
data(iris)
# 将iris数据集中的前三列作为三个维度
x <- iris$Sepal.Length
y <- iris$Sepal.Width
z <- iris$Petal.Length
# 将iris数据集中Species列对应到颜色上面,并设置自定义颜色
color <- as.factor(iris$Species)
mycolor <- c("blue", "red", "green")
# 绘制三维散点图
plot_ly(x = x, y = y, z = z, color = color, colors = mycolor, type = "scatter3d", mode = "markers",
marker=list(size = 2)) %>%
layout(scene = list(xaxis = list(title = "Sepal Length"),yaxis = list(title = "Sepal Width"),zaxis = list(title = "Petal Length")))
# 将三维散点图保存为交互式HTML文件
saveWidget(plot_ly(x = x, y = y, z = z, color = color, colors = mycolor, type = "scatter3d", mode = "markers",
marker=list(size = 2)) %>%
layout(scene = list(xaxis = list(title = "Sepal Length"), yaxis = list(title = "Sepal Width"), zaxis = list(title = "Petal Length"))),
file = "iris_visualization.html",
selfcontained = TRUE)
3.2 三维线图
library(plotly)
# 从 iris 数据集中选择前三个变量,并添加一个类别变量
df <- iris[, 1:3]
df$Species <- as.factor(iris$Species) # 修改列名的首字母为大写
# 绘制一个三维线图
plot_ly(df, x = ~Sepal.Length, y = ~Sepal.Width, z = ~Petal.Length, color = ~Species) %>%
add_lines() %>%
layout(scene = list(xaxis = list(title = "Sepal Length"), yaxis = list(title = "Sepal Width"), zaxis = list(title = "Petal Length")))
3.3 k-means聚类
k-means是一种聚类算法,常用于对数据集进行无监督的聚类分析。该算法将数据集划分为k个不同的类别,如何划分依据的是数据之间的相似性。k-means算法将数据划分为k个簇,簇与簇之间的距离尽可能大,簇内的点与簇中心之间的距离尽可能小。
# 取出数据集中的4个特征列作为聚类的输入
X <- iris[,1:4]
# 设定聚类的类别数量为3
k <- 3
# 使用k均值聚类算法进行聚类
km <- kmeans(X, k)
3.4 plotly包绘图
# 将iris数据集中的Species列由字符型变为数值型,以便与聚类结果进行比较
species <- as.integer(factor(iris$Species)) - 1
# 绘制聚类的结果和原始数据在三维空间中的散点图
plot_ly(x=X$Sepal.Length, y=X$Sepal.Width, z=X$Petal.Length,
color=factor(km$cluster), colors=c("red", "blue", "green"),
symbol=factor(species), symbols=c(0, 1, 2), marker=list(size=3)) %>%
add_markers() %>%
add_markers(x=km$centers[,1], y=km$centers[,2], z=km$centers[,3], color=c("red", "blue", "green"),
symbol=c(3,3,3), size=10) %>%
layout(scene = list(xaxis = list(title = "Sepal Length"),
yaxis = list(title = "Sepal Width"),
zaxis = list(title = "Petal Length")))
本文由博客一文多发平台 OpenWrite 发布!