R语言数据可视化教程(ggplot2)

您所在的位置:网站首页 r语言绘图ggplot R语言数据可视化教程(ggplot2)

R语言数据可视化教程(ggplot2)

2023-05-20 22:50| 来源: 网络整理| 查看: 265

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