R语言数据可视化教程(ggplot2) |
您所在的位置:网站首页 › r语言绘图ggplot › R语言数据可视化教程(ggplot2) |
R 语 言数据可 视 化教程( ggplot2 ) _ 绘 制散点 图 # 5. 散点 图 #
散点 图 通常用来刻画两个 连续 型 变 量之 间 的关系 # 5.1 绘 制散点 图 #
运行 geom_point() 函数,分 别 映射一个 变 量到 x 和 y library(gcookbook) library(ggplot2) #
列出用到的列 heightweight[,c("ageYear","heightIn")] ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point() #
通 过设 定点型( shape )参数可以在散点 图 中 绘 制默 认值 以外的点型。 ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point(shape=21) #
大小( size) 参数可以控制 图 中点的大小。系 统 默 认 的大小( size) 值 等于 2 ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point(size=1.5) # 5.2 使用点形和 颜 色属性,并基于某 变 量 对 数据 进 行分 组 #
将分 组变 量映射 给 点形( shape) 和 颜 色( colours) 属性。 library(gcookbook) heightweight[,c("sex","ageYear","heightIn")] ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point() ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex))+geom_point() #
分 组变 量必 须 是分 类变 量, 换 言之,它必 须 是因子型或者字符串型的向量。如果分 组变 量以数 值 型 变 量 进 行存 储 #
, 则 需要将它 转 化 为 因子型 变 量之后,才能以其作 为 分 组变 量 #
可以将一个 变 量同 时 映射 给 shape 和 colour 属性。当有多个分 组变 量 时 可以将它 们 分 别 映射 给这 两个 图 形属性 ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex,colour=sex))+geom_point() #
通 过调 用 scale_shape_manual() 函数可以使用其他点形; 调 用 scale_colour_brewer() 或者 scale_colour_manual() 函数可以使用其他 调 色板 ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex,colour=sex))+geom_point()+scale_shape_manual(values = c(1,2))+scale_color_brewer(palette = "Set1") # 5.3 使用不同于默 认设 置的点形 #
通 过 指定 geom_point() 函数的点形( shape) 参数可以 设 定散点 图 中所有数据点的点形 library(gcookbook) ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point(shape=3) #
如果已将分 组变 量映射 给 shape, 则 可以 调 用 scale_shape_manual() 函数来修改点形 #
使用略大且自定 义 点形的数据点 ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex))+geom_point(size=3)+scale_shape_manual(values = c(1,4)) #
生成一个数据副本 hw |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |