R可视化和ggplot2

《R数据可视化手册》学习笔记5---散点图(3)点型、大小和颜色

2023-09-25  本文已影响0人  RSP小白之路

写在前面。


图形属性

设置或者映射变量给图形属性可以展示更多的信息并使图形更美观和丰富。

散点图的属性包括,点的形状颜色透明度位置标签等。

下文分别介绍。

点型、大小和颜色

可以在geom_point()中指定点型、颜色;也可以在aes语句中给各属性映射变量。

映射变量给属性

示例数据heightweight数据集。

> str(heightweight)
'data.frame':   236 obs. of  5 variables:
 $ sex     : Factor w/ 2 levels "f","m": 1 1 1 1 1 1 1 1 1 1 ...
 $ ageYear : num  11.9 12.9 12.8 13.4 15.9 ...
 $ ageMonth: int  143 155 153 161 191 171 185 142 160 140 ...
 $ heightIn: num  56.3 62.3 63.3 59 62.5 62.5 59 56.5 62 53.8 ...
 $ weightLb: num  85 105 108 92 112 ...

使用sex对点进行分类。

ggplot(data = heightweight ,aes(x = ageYear, y = heightIn, shape = sex)) + geom_point()

[图片上传失败...(image-bfe054-1695714583990)]

ggplot(data = heightweight ,aes(x = ageYear, y = heightIn, colour = sex)) + geom_point()

[图片上传失败...(image-c034c1-1695714583990)]


设置非默认的颜色和点型

可以使用scale_shape_manual()设置其它点型;

调用scale_colour_manual()或者scale_colour_brewer()使用其他调色板。

ggplot(data = heightweight ,aes(x = ageYear, y = heightIn, shape = sex, colour = sex)) + 
  geom_point() +
  scale_shape_manual(values =c(1,2)) +
  scale_colour_brewer(type="seq",palette="Set1")

[图片上传失败...(image-e236ad-1695714583990)]

这些语句中的参数可以看R中相关的帮助文档。


上一篇 下一篇

猜你喜欢

热点阅读