引言
散点图是描绘两个连续型变量之间关系的图形,特别是在观察两个变量之间的相关关系时特别好使。
散点图基本操作
aes中的x,y值分别表示在x,y轴的变量;geom_point表示增加三点图图层,其中的size控制点的大小,shape控制形状,一共25个,为0-25。
library(gcookbook)
library(ggplot2)
head(heightweight)
# sex ageYear ageMonth heightIn weightLb
#1 f 11.92 143 56.3 85.0
#2 f 12.92 155 62.3 105.0
#3 f 12.75 153 63.3 108.0
#4 f 13.42 161 59.0 92.0
#5 f 15.92 191 62.5 112.5
#6 f 14.25 171 62.5 112.0
ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point(size=3,shape=21)
分组散点图
散点图分组有两种方式,一种利用shape,以点的形状来区分各种;一种用color,以点的颜色来区分.但是得记住,分组的变量必须为因子变量或者字符串。
ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) + geom_point()#以颜色区分
ggplot(heightweight,