ggplot2 —— 绘图R包

R的基础包里面也有很多画图函数,例如plot();barplot();qqplot(); 
但是还有大名鼎鼎的ggplot2包,用这个包的函数画出的图比较漂亮,而且使用灵活。

在ggplot的官方手册中提及到, 一张统计图形是由从数据几何对象(geometric object,记为geom,如点,线,条形等),图形属性(aesthetic attributes,记为aes,如颜色,形状,大小)的一个映射。除此以外,图形还可以包含了数据的统计变换(statistical transformation, 记写为stats)。最后,绘画在某个坐标系中(coordinate system, 记为coord),而分面(facet,将绘图窗口分成若干个子窗口)是用来生成数据中不同子集的图形 
先介绍下它的基本元素:

  • 数据与映射
  • 几何对象geom
  • 统计变化stats
  • 标度
  • 坐标系coord
  • 分面facet 
    这些组件之间是通过“+”, 以图层(layer)的方式来粘合构图的, 所以图层是ggplot2中一个重要的概念。 
    以下用的数据是一份毕业生数据,来自王斌会主编的《数据分析与R语言建模》的练习数据,一共48个样本点,9个属性 
    一、数据 
    在ggplot2中,接受的数据集必须是以data.frame格式的。这种格式易于保存数据,而且能在保留原有的绘图参数下, 用%+%方便地变更已有数据集。
library("ggplot2")#调用包
UG=read.table("clipboard",header=T);
head(UG)
p=ggplot(UG,aes(score,income),color=sex)+geom_point()
UG.c=transform(UG,income=income*1.5)#将收入放大1.5倍,其他不变
p %+%UG.c
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

第一张 
收入放大1.5倍的

二、映射 
aes()函数是ggplot2中的映射函数, 映射是数据集中的数据关联到相应的图形属性过程中一种对应关系

1.映射的概念

>p=ggplot(UG,aes(score,income,color=sex))+geom_point()
> summary(p)
data: id, name, sex, region, birth, income, height, weight, score
  [48x9]
mapping:  x = score, y = income, colour = sex
> p1=ggplot(data=UG)
> summary(p1)
data: id, name, sex, region, birth, income, height, weight, score
  [48x9]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以发现,在p中指定了x轴为score,y轴为income,颜色为sex,这与p1中的不同

2.设定与映射 
映射将一个变量中离散或连续的数据与一个图形属性中以不同的参数来相互关联, 而设定能够将这个变量中所有的数据统一为一个图形属性。

p2=ggplot(UG,aes(score,income))
p2+geom_point(color="blue")#设定散点的颜色为蓝色
p2+geom_point(aes(color="blue"))
  • 1
  • 2
  • 3
  • 4

设定为蓝色点 
出现错误 
最后一句出现了错误,是因为在aes中, color = “blue”的实际意思是把”blue”当为一个变量, 用这个变量里的数据去关联图形属性中的参数, 而”blue”只含有一个字符变量, 默认情况下为离散变量, 按默认的颜色标度标记为桃红色

比较以下三种方法

ggplot(UG,aes(score,income),colour=sex)+geom_point()
ggplot(UG,aes(score,income,colour=sex))+geom_point()
ggplot(UG,aes(score,income))+geom_point(aes(colour=sex)) 
  • 1
  • 2
  • 3

第一种的点是黑色点,第二种和第三种都是按照性别这个变量分颜色,第三种比较好记忆,相当于先画好图,再加上带颜色的散点。

3.分组 
是ggplot2种映射关系的一种, 默认情况下ggplot2把所有观测点分为了一组, 如果需要把观测点按额外的离散变量进行分组处理, 必须修改默认的分组设置。

三、图层 
1.在几何对象中设定映射 
我们可以在在ggplot()中设定了映射了关系, 这种映射关系是默认的, 也可以在后面的几何对象中沿用已设定的默认映射关系, 也可以随时在几何对象中进行更改。 
下面用到一个diamonds数据集,这个数据集的样本数非常大,所以要先抽样,这样画出的图才比较好看。

data(diamonds)
head(diamonds)
set.seed(74)#设定随机种子
small.diamonds=diamonds[sample(nrow(diamonds),500),]
#提取数据
head(small.diamonds)
dp =ggplot(small.diamonds, aes(x = carat, y = price, color = factor(color)))#设定默认的映射关系
dp + geom_point()#沿用默认的映射关系来绘制散点图
dp + geom_point(aes(shape = factor(cut))) #添加图层中的shape的映射关系
dp + geom_point(aes(y = cut)) #修改默认的y的映射关系, 注意图中y轴名称仍然以默认的price表示
dp + geom_point(aes(color = NULL))#删除默认的color映射关系
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

按照钻石的颜色分类不同颜色的散点 
既有钻石颜色,又有钻石切割的分类 
钻石颜色 
去掉各种颜色的映射关系

注意体会第二和第三种图的画法 
四、几何对象

dp =ggplot(small.diamonds, mapping=aes(x =carat, y = price,shape=cut,color = factor(color)))#设定默认的映射关系
dp + geom_point()
  • 1
  • 2

前面的钻石数据集的第二幅图也可以用这两个语句搞定,这里有点区别在于前面的是先画好了ggplot,再加上不同映射的散点;而这里是先画好了带有不同映射的ggplot,再加上点就好。

1.直方图

#直方图
ggplot(small.diamonds)+geom_histogram(aes(x=price))
  • 1
  • 2

直方图 
还可以按照不同的变量填充不同色,比如切工、钻石颜色

ggplot(small.diamonds)+geom_histogram(aes(x=price,fill=cut))
ggplot(small.diamonds)+geom_histogram(aes(x=price,fill=color))
  • 1
  • 2

按照不同切工分配颜色 
按照不同颜色的钻石分配颜色

2.柱形图

#柱形图,按照不同的变量
ggplot(small.diamonds)+geom_bar(aes(x=clarity))
ggplot(small.diamonds)+geom_bar(aes(x=color))
  • 1
  • 2
  • 3

这里写图片描述 
这里写图片描述 
注意直方图和柱形图的区别:直方图把连续型的数据按照一个个等长的分区(bin)来切分,然后计数,画柱状图。而柱状图是分类数据,按类别计数

3.密度函数图

#密度函数图
ggplot(small.diamonds)+geom_density(aes(x=price,color=clarity))#color指定颜色
ggplot(small.diamonds)+geom_density(aes(x=price,fill=cut))#fill在下方填充
  • 1
  • 2
  • 3

这里写图片描述 
这里写图片描述

4.箱线图

#箱线图
ggplot(small.diamonds)+geom_boxplot(aes(x=cut,y=price,fill=clarity))
  • 1
  • 2

这里写图片描述

在ggplot中还有许多geom_xxx的函数,

geom_ablinegeom_area
geom_bargeom_bin2d
geom_blankgeom_boxplot
geom_contourgeom_crossbar
geom_densitygeom_density2d
geom_dotplotgeom_errorbar
geom_errorbarhgeom_freqpoly
geom_hexgeom_histogram
geom_hlinegeom_jitter
geom_linegeom_linerange
geom_mapgeom_path
geom_pointgeom_pointrange
geom_polygongeom_quantile
geom_rastergeom_rect
geom_ribbongeom_rug
geom_segmentgeom_smooth
geom_stepgeom_text
geom_tilegeom_violin
geom_vline 
 

五、标度

#标度
>ggplot(small.diamonds)+geom_point(aes(x=carat,y=price,shape=cut,color=color))
>ggplot(small.diamonds)+geom_point(aes(x=carat,y=price,shape=cut,color=color))+scale_y_log10()+scale_color_manual(values=rainbow(7))#对y变量做了对数变换
  • 1
  • 2
  • 3

对比下两中做法 
没有标度变换 
做了对数变换,而且取色也做了变换

六、统计变换 
统计变换对原始数据进行某种计算,然后在图上表示出来。

例如对散点图上加一条回归线

#统计变换
ggplot(small.diamonds,aes(x=carat,y=price))+geom_point()+scale_y_log10()+stat_smooth()
  • 1
  • 2

这里写图片描述

还有一些统计变换可选的,如下表

stat_ablinestat_identity
stat_binstat_qq
stat_bin2dstat_quantile
stat_bindotstat_smooth
stat_binhexstat_spoke
stat_boxplotstat_sum
stat_contourstat_summary
stat_densitystat_summary2d
stat_density2dstat_summary_hex
stat_ecdfstat_unique
stat_functionstat_vline
stat_hlinestat_ydensity
 

七、坐标系统 
1.用coord_flip()实现坐标轴翻转

#坐标系统
ggplot(small.diamonds,aes(x=clarity,fill=clarity))+geom_bar()
ggplot(small.diamonds,aes(x=clarity,fill=clarity))+geom_bar()+coord_flip()
  • 1
  • 2
  • 3

这里写图片描述 
这里写图片描述

2.用coord_polar()实现转换极坐标

#极坐标
>ggplot(small.diamonds)+geom_bar(aes(x=factor(1),fill=cut))+coord_polar(theta="y")
#x其实是上面的clarity,是一个因子变量
  • 1
  • 2
  • 3

这里写图片描述 
其实,可以看出,极坐标下的条形图就是饼图。

#靶心图
ggplot(small.diamonds)+geom_bar(aes(x=factor(1),fill=cut))+coord_polar()
  • 1
  • 2

这里写图片描述

#风玫瑰图
ggplot(small.diamonds)+geom_bar(aes(x=clarity,fill=cut))+coord_pola
  • 1
  • 2

这里写图片描述

八、分面(facet) 
按照不同的透明度,分别回归(克拉和价格作回归),用分面

#分面,这是一行代码,这里特别注意,x和y的指定要放在ggplot中
>ggplot(small.diamonds,aes(x=carat,y=price,color=clarity))+geom_point()+scale_y_log10()+facet_wrap(~clarity)+stat_smooth()
  • 1
  • 2

这里写图片描述

九、主题 
对图进行定制,如title, xlab, ylab显示出图标题,x轴,y轴,ggplot2提供了ggtitle(), xlab()和ylab()来实现。除此之外,我们可能还需要改变字体,字体大小,坐标轴,背景等各种元素,这需要通过theme()函数来完成。 
ggplot2还提供一些已经写好的主题,如theme_grey()为默认主题,theme_bw()为白色背景的主题,还有theme_classic()主题

theme_economisttheme_economist_white
theme_wsjtheme_excel
theme_fewtheme_foundation
theme_igraytheme_solarized
theme_statatheme_tufte
 
#主题颜色
install.packages("ggthemes")
library("ggthemes")
ggplot(small.diamonds)+geom_boxplot(aes(x=cut,y=price,fill=clarity))+theme_wsj()
转载地址 :https://blog.csdn.net/dollyh/article/details/74332208
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值