BMI=read.table('/Users/zhangzhishuai/Downloads/33 lesson33 ggplot2散点图(一)/33_ggplot2/BMI.txt',sep = '\t', header = T,row.names = 1)
library(ggplot2)
# 散点图
ggplot(BMI,aes(x=height,y=weight))
ggplot(BMI,aes(x=height,y=weight)) + geom_point()
# 控制点大小
ggplot(BMI,aes(x=height,y=weight)) + geom_point(size=4)
# 控制点的形状
ggplot(BMI,aes(x=height,y=weight)) + geom_point(shape=15)
# 同时设置点的形状和大小
ggplot(BMI,aes(x=height,y=weight)) + geom_point(shape=15, size=4)
# 利用形状来标识分组
ggplot(BMI,aes(x=height,y=weight,shape=gender)) + geom_point(size=4)
ggplot(BMI,aes(x=height,y=weight,shape=gender)) +
geom_point(size=4) +
scale_shape_manual(values = c(5,17)) #控制形状
# 同时利用形状和颜色进行分组
ggplot(BMI,aes(x=height,y=weight,shape=gender,
color = gender)) + #同时设置形状的颜色
geom_point(size=4) +
scale_shape_manual(values = c(5,17)) + #控制形状
scale_color_manual(values = c('red','blue')) #控制颜色
ggplot(BMI,aes(x=height,y=weight,shape=gender,
color = gender)) + #同时设置形状的颜色
geom_point(size=4) +
scale_shape_manual(values = c(5,17)) + #控制形状
scale_color_brewer(palette = "Set1") #控制颜色
# 设置渐变填充色
ggplot(BMI,aes(x=height,y=weight,fill=age)) +
geom_point(size=4,shape=21) +
scale_fill_gradient(low='yellow',high = 'red') #设置渐变色
# 利用区段显示填充色
ggplot(BMI,aes(x=height,y=weight,fill=age)) +
geom_point(size=4, shape=21) +
scale_fill_gradient(low = 'yellow',
high = 'red',
breaks=seq(28,44,length.out=5),
guide = guide_legend()
BMI=read.table('/Users/zhangzhishuai/Downloads/33 lesson33 ggplot2散点图(一)/33_ggplot2/BMI.txt',sep = '\t', header = T,row.names = 1)
library(ggplot2)
# 散点图
ggplot(BMI,aes(x=height,y=weight))
ggplot(BMI,aes(x=height,y=weight)) + geom_point()
# 控制点大小
ggplot(BMI,aes(x=height,y=weight)) + geom_point(size=4)
# 控制点的形状
ggplot(BMI,aes(x=height,y=weight)) + geom_point(shape=15)
# 同时设置点的形状和大小
ggplot(BMI,aes(x=height,y=weight)) + geom_point(shape=15, size=4)
# 利用形状来标识分组
ggplot(BMI,aes(x=height,y=weight,shape=gender)) + geom_point(size=4)
ggplot(BMI,aes(x=height,y=weight,shape=gender)) +
geom_point(size=4) +
scale_shape_manual(values = c(5,17)) #控制形状
# 同时利用形状和颜色进行分组
ggplot(BMI,aes(x=height,y=weight,shape=gender,
color = gender)) + #同时设置形状的颜色
geom_point(size=4) +
scale_shape_manual(values = c(5,17)) + #控制形状
scale_color_manual(values = c('red','blue')) #控制颜色
ggplot(BMI,aes(x=height,y=weight,shape=gender,
color = gender)) + #同时设置形状的颜色
geom_point(size=4) +
scale_shape_manual(values = c(5,17)) + #控制形状
scale_color_brewer(palette = "Set1") #控制颜色
# 设置渐变填充色
ggplot(BMI,aes(x=height,y=weight,fill=age)) +
geom_point(size=4,shape=21) +
scale_fill_gradient(low='yellow',high = 'red') #设置渐变色
# 利用区段显示填充色
ggplot(BMI,aes(x=height,y=weight,fill=age)) +
geom_point(size=4, shape=21) +
scale_fill_gradient(low = 'yellow',
high = 'red',
breaks=seq(28,44,length.out=5),
guide = guide_legend())
# 利用变量控制点大小
ggplot(BMI,aes(x=height,y=weight,size=age)) +
geom_point()
# 利用变量设置点大小和颜色
ggplot(BMI,aes(x=height,y=weight,size=age,color=BMI)) +
geom_point()
# 设置渐变色
ggplot(BMI,aes(x=height,y=weight,size=age,color=BMI)) +
geom_point() +
scale_color_gradient(low = 'yellow',high = 'red')
# 添加线性拟合直线
p = ggplot(BMI,aes(x=height,y=weight))
p + geom_point(
colour = 'grey60',size=4
) + stat_smooth(method = lm,
se=FALSE, # 取消置信区间
colour='red', # 设置直线颜色
size = 3 # 设置直线宽度
)
model <- lm(weight~height,BMI)
summary(model)
# 添加文字注释
p + geom_point(
colour = 'grey60',
size=4
) + stat_smooth(method = lm,
se=FALSE, # 取消置信区间
colour='red', # 设置直线颜色
size = 3 # 设置直线宽度
) + annotate('text',
label='r^2=0.8848\npvalue=0.006039', # 添加R方和P值
x=162, #横坐标
y=75 # 纵坐标
) + annotate('text',
x=180,y=76,label='tom',colour='blue',cex=6
) + annotate('text',
x=165,y=59,label='cindy',colour='red',cex=5)
p + geom_point() +
geom_text(vjust=-0.6, # 竖直方向调
label=rownames(BMI) #
)
p + geom_point() +
geom_text(label=round( # 保留两位小数
BMI$BMI,2), # 显示数值
vjust=-0.6)
示例输入数据:
name height weight gender BMI age
tom 180 75 male 23.14814815 38
cindy 165 58 female 21.30394858 45
jimmy 175 72 male 23.51020408 43
sam 173 68 male 22.72043837 35
lucy 160 60 female 23.4375 32
lily 163 55 female 20.2020202 28