图片的最大价值在于促使我们发现从未预料到的事 ——John Tukey
图形属性
图形属性是图中对象的可视化属性,其中包括数据点的大小、形状和颜色。下面这段代码:
ggplot(mpg)+geom_point(aes(displ,hwy,color=class))
color = class
,也可以写为colour = class
,这里指的是把点的颜色映射为变量class

标度变换
对于变量class中不同的变量类型(值),都有唯一的颜色来对应,这个过程我们称作标度变换。
class映射为size
如果把无序变量class映射为有序图形属性size:
ggplot(mpg)+geom_point(aes(displ,hwy,size=class))

这里会显示警告信息,Warning message:Using size for a discrete variable is not advised.
指的是这种方法不太可取。
class映射为alpha(透明度)或shape(形状)
ggplot(mpg)+geom_point(aes(displ,hwy,alpha=class))

ggplot(mpg)+geom_point(aes(displ,hwy,shape=class))

ggplot2只能同时出现6种形状,多出的变量值不会出现在图中。
设置所有点集为蓝色
ggplot(mpg)+geom_point(aes(displ,hwy),color="blue")

颜色名称是一个字符串,而字符串常量是由一对双引号括起的字符序列,所以color="blue"加双引号。
R的25种内置形状

输入相应的数字编号,可以获得对应的形状
ggplot(mpg)+geom_point(aes(displ,hwy),shape='数字编号')
比如,15号为黑色正方形:
ggplot(mpg)+geom_point(aes(displ,hwy),shape=15)

网友评论