R语言基础复习
R 中的基本类型:
- 数字 (numeric, as.numeric), 包括整数 (integer, as.integer) 和浮点数 (double, as.double) 等
- 字符(character, as.character)
- 逻辑, TRUE (T) 和 FALSE (F), T 和 F 是简写, 交互时方便操作
- 时间
- 特殊值, NA, NaN, Inf
R 中常见数据结构:
- 向量 (vector, as.vector), 为一维, 向量中的每一个元素都是相同的类型. 有数字向量 (numeric, as.numeric), 字符串向量(character, as.character)等.
- 因子 (factor), 有重复值的向量可以存储为因子, 节约内存, 因子有 level 和 label 属性.
- 数组, 多维度, 每一个元素都是相同的类型. 矩阵为二维的数组.
- 列表 (list), 一维, 元素可以是不同的类型.
- 数据框 (data.frame), 具有列表和矩阵的特点, 二维, 不同列可以是不同的类型, 同列的数据是相同的类型。
ggplot画散点图
1、aes()函数
用于定义图形对象的属性与数据的映射关系。
主要用法有以下几种:
aes(x = x_var, y = y_var)
:指定X轴和Y轴的变量,其中x_var
和y_var
是数据框中的列名称(或者是表达式,例如:x = 2 * y
)。aes(colour = var)
:指定颜色的变量,其中var
是数据框中的一个列,用于表示各个数据点对应的颜色属性。aes(fill = var)
:指定填充色的变量,类似于colour
参数,但是用于表示需要填充的颜色属性(例如柱状图中的填充)。aes(shape = var)
:指定散点图点的形状,其中var
是数据框中的一个列,用于表示各个数据点对应的点形状。aes(size = var)
:指定点或线条的尺寸,其中var
是数据框中的一个列,用于表示各个数据点对应的点或线条的尺寸大小。
应用实例间多元正态分布数据。aes()是找出分类变量来画图的。
2、geom_point
常用参数:
alpha
:透明度colour
:点颜色fill
:填充色group
:分组变量shape
:形状size
:大小stroke
:边框大小
模拟生成不同类型的多元正态分布数据
1、利用MASS包中的mvrnorm()函数生成
- 能完全分开的球状数据
- 不能完全分开的球状数据
- 样本大小不等的球状数据
- 并排拉长数据
- 非球状数据
library(MASS) library(ggplot2) #能完全分开的球状数据 set.seed(1) miu_1<-c(0,0) miu_2<-c(8,0) miu_3<-c(4,8) sigma_1 <- matrix(c(1,0,0,1),2,2) sigma_2 <- matrix(c(1,0,0,1),2,2) sigma_3 <- matrix(c(1,0,0,1),2,2) x_1=mvrnorm(n=50,miu_1, sigma_1) x_2=mvrnorm(n=50,miu_2, sigma_2) x_3=mvrnorm(n=50,miu_3, sigma_3) data <- data.frame(x = c(x_1[, 1], x_2[, 1], x_3[, 1]), y = c(x_1[, 2], x_2[, 2], x_3[, 2]), group = rep(c("Group 1", "Group 2", "Group 3"), each = 50)) ggplot(data, aes(x = x, y = y, color = group)) + geom_point()+ scale_color_manual(values = c("red", "yellow", "blue")) + ggtitle("能完全分开的球状数据") #不能完全分开的球状数据 miu_1<-c(0,0) miu_2<-c(3,0) miu_3<-c(1,2) x_1=mvrnorm(n=50,miu_1, sigma_1) x_2=mvrnorm(n=50,miu_2, sigma_2) x_3=mvrnorm(n=50,miu_3, sigma_3) data <- data.frame(x = c(x_1[, 1], x_2[, 1], x_3[, 1]), y = c(x_1[, 2], x_2[, 2], x_3[, 2]), group = rep(c("Group 1", "Group 2", "Group 3"), each = 50)) ggplot(data, aes(x = x, y = y, color = group)) + geom_point()+ ggtitle("不能完全分开的球状数据") #样本大小不等的球状数据 miu_1<-c(1,0) miu_2<-c(6,0) miu_3<-c(3,4) sigma_1 <-0.25* matrix(c(1,0,0,1),2,2) sigma_2 <-4* matrix(c(1,0,0,1),2,2) sigma_3 <-matrix(c(1,0,0,1),2,2) x_1=mvrnorm(n=20,miu_1, sigma_1) x_2=mvrnorm(n=80,miu_2, sigma_2) x_3=mvrnorm(n=40,miu_3, sigma_3) data <- data.frame(x = c(x_1[, 1], x_2[, 1], x_3[, 1]), y = c(x_1[, 2], x_2[, 2], x_3[, 2]), group = rep(c("Group 1", "Group 2", "Group 3"),times = c(20, 80, 40))) ggplot(data, aes(x = x, y = y, color = group)) + geom_point()+ ggtitle("样本大小不等的球状数据") #并排拉长数据 miu_1<-c(8,8) miu_2<-c(2,14) sigma_1 <-matrix(c(37,35,35,37),2,2) sigma_2 <-matrix(c(37,35,35,37),2,2) x_1=mvrnorm(n=50,miu_1, sigma_1) x_2=mvrnorm(n=50,miu_2, sigma_2) data <- data.frame(x = c(x_1[, 1], x_2[, 1]), y = c(x_1[, 2], x_2[, 2]), group = rep(c("Group 1", "Group 2"),each=50)) ggplot(data, aes(x = x, y = y, color = group)) + geom_point()+ ggtitle("并排拉长的数据") #非球状数据 x_1<-data.frame(rnorm(50,1.4,0.9)) x_2<-data.frame(rnorm(50,3.5,0.64)) e1<-rnorm(50,0,0.09) e2<-rnorm(50,0,0.09) colnames(x_1)<-c("x_1") colnames(x_2)<-c("x_2") for (i in 1:50) { x_1$y_1[i]<-sin(x_1$x_1[i])+e1[i] } for (i in 1:50) { x_2$y_2[i]<-cos(x_2$x_2[i])+e2[i] } x_1$group_1 ="group_1" x_2$group_2="group_2" colnames(x_1)<-c("x","y","group") colnames(x_2)<-c("x","y","group") data<-rbind(x_1,x_2) ggplot(data,aes(x=x,y=y,color = group))+ geom_point()+ ggtitle("非球状数据")
生成非球状数据的part受益很大,更熟悉R
group那一组变量对应的就是分组变量,用each or times.
2、scale_color_manual()
如果想要根据分类变量指定具体颜色,就加个scale_color_manual()
函数来手动指定颜色映射。
library(MASS)
library(ggplot2)
#能完全分开的球状数据
set.seed(1)
miu_1<-c(0,0)
miu_2<-c(8,0)
miu_3<-c(4,8)
sigma_1 <- matrix(c(1,0,0,1),2,2)
sigma_2 <- matrix(c(1,0,0,1),2,2)
sigma_3 <- matrix(c(1,0,0,1),2,2)
x_1=mvrnorm(n=50,miu_1, sigma_1)
x_2=mvrnorm(n=50,miu_2, sigma_2)
x_3=mvrnorm(n=50,miu_3, sigma_3)
data <- data.frame(x = c(x_1[, 1], x_2[, 1], x_3[, 1]),
y = c(x_1[, 2], x_2[, 2], x_3[, 2]),
group = rep(c("Group 1", "Group 2", "Group 3"), each = 50))
ggplot(data, aes(x = x, y = y, color = group)) +
geom_point()+
scale_color_manual(values = c("red", "yellow", "blue")) +
ggtitle("能完全分开的球状数据")