![](https://img.haomeiwen.com/i25702203/f0239e98b9124c78.png)
![](https://img.haomeiwen.com/i25702203/09783428fb352109.png)
![](https://img.haomeiwen.com/i25702203/28ecef15ef8bcc19.png)
基础包作图函数
plot(iris[,1],iris[,3],col = iris[,5])
text(6.5,4, labels = 'hello') #text中6.5和4为hello的坐标
![](https://img.haomeiwen.com/i25702203/12c520493d0fddb1.png)
boxplot(iris[,1]~iris[,5])
![](https://img.haomeiwen.com/i25702203/e4d8a996d9633231.png)
关闭画板
dev.off()
ggplot2绘图
1.入门级绘图模板
if(!require(ggplot2))install.packages('ggplot2')
library(ggplot2)
test = iris
#1.入门级绘图模板
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
![](https://img.haomeiwen.com/i25702203/43ab6fad62f0a952.png)
2.映射-颜色、大小、透明度、形状
![](https://img.haomeiwen.com/i25702203/cdcbdf53b3bfba82.png)
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
![](https://img.haomeiwen.com/i25702203/593e9841f29c7ae3.png)
![](https://img.haomeiwen.com/i25702203/4aeff69b6c00bee2.png)
3.分面
ggplot(data = test) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species)
![](https://img.haomeiwen.com/i25702203/eeeccc2f96aceb4c.png)
#双分面
test$Group = sample(letters[1:5],150,replace = T) #给数据test新加向量Group,Group为5个可重复字母,共150个
ggplot(data = test) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_grid(Group ~ Species)
![](https://img.haomeiwen.com/i25702203/8423d4c2ff9ba5c2.png)
练习走一波
# 示例数据:ggplot2中数据集mpg
View(mpg)
![](https://img.haomeiwen.com/i25702203/8f8990aa4cc5f861.png)
# 1.分别以mpg的displ和hwy两列作为横纵坐标,画点图。
ggplot(data = mpg)+
geom_point(aes(x=displ,y=hwy))
#或者用下列代码
ggplot(data = mpg,aes(x=displ,y=hwy))+
geom_point()
![](https://img.haomeiwen.com/i25702203/f1335fd04eb36f62.png)
# 2.尝试修改颜色或大小,从mpg数据框中任选可以用来分类的列。
ggplot(data = mpg)+
geom_point(aes(x=displ,y=hwy,color=displ))
#或者用下列代码
ggplot(data = mpg,aes(x=displ,y=hwy,color=displ))+
geom_point()
![](https://img.haomeiwen.com/i25702203/f34bd9a20c059848.png)
# 3.根据class列来分面
ggplot(data = mpg)+
geom_point(aes(x=displ,y=hwy,color=displ))+
facet_wrap(~class)
#或者用下列代码
ggplot(data = mpg,aes(x=displ,y=hwy,color=displ))+
geom_point()+
facet_wrap(~class)
![](https://img.haomeiwen.com/i25702203/2e355a6de029e925.png)
# 4.根据drv和cyl两个变量来分面
ggplot(data = mpg)+
geom_point(aes(x=displ,y=hwy,color=displ))+
facet_grid(drv~cyl)
#或者用下列代码
ggplot(data = mpg,aes(x=displ,y=hwy,color=displ))+
geom_point()+
facet_grid(drv~cyl)
![](https://img.haomeiwen.com/i25702203/63995acc77b96571.png)
4.几何对象
![](https://img.haomeiwen.com/i25702203/a45d611ee61c1ee8.png)
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))
![](https://img.haomeiwen.com/i25702203/e1a1c80b9aef91e9.png)
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length,
group = Species))
![](https://img.haomeiwen.com/i25702203/9285f114d26220f8.png)
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
![](https://img.haomeiwen.com/i25702203/8e4268ac1f66b786.png)
![](https://img.haomeiwen.com/i25702203/e91bbb2b96b33653.png)
#局部映射
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
![](https://img.haomeiwen.com/i25702203/1528cdf1314d3c26.png)
#全局映射
ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
geom_smooth()+
geom_point()
![](https://img.haomeiwen.com/i25702203/d36283edbc70968d.png)
# 1.尝试写出下图的代码
# 数据是iris
# X轴是Species
# y轴是Sepal.Width
# 图是箱线图
ggplot(iris,aes(x=Species,y=Sepal.Width))+
geom_boxplot()
![](https://img.haomeiwen.com/i25702203/ab1087414d4418e8.png)
# 2. 尝试在此图上叠加点图,
# 能发现什么问题?
ggplot(iris,aes(x=Species,y=Sepal.Width))+
geom_boxplot()+
geom_point()
![](https://img.haomeiwen.com/i25702203/e3deac6de221dc3f.png)
# 3.用下列代码作图,观察结果
ggplot(test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
geom_point()+
geom_smooth(color = "black")
![](https://img.haomeiwen.com/i25702203/650179c1a41701c7.png)
当局部映射和全局映射冲突,以局部映射为先。
5.统计变换
View(diamonds)
![](https://img.haomeiwen.com/i25702203/92e1a4c8606aa8ef.png)
![](https://img.haomeiwen.com/i25702203/1362447684808577.png)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
![](https://img.haomeiwen.com/i25702203/51cf257cc0ce07a0.png)
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))
![](https://img.haomeiwen.com/i25702203/bcf86e9cf39c681b.png)
#统计变换使用场景
#5.1.不统计,数据直接做图
fre = as.data.frame(table(diamonds$cut))
fre
![](https://img.haomeiwen.com/i25702203/4fb6e439019ac2c4.png)
#使用表中数据直接作图而不统计
ggplot(data = fre) +
geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity") #stat = "identity"设定为不统计
![](https://img.haomeiwen.com/i25702203/f62715f162005f56.png)
#5.2count(计数)改为prop(百分比)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
![](https://img.haomeiwen.com/i25702203/d5516766b7c22880.png)
6.位置调整
View(mpg)
![](https://img.haomeiwen.com/i25702203/f1dc6eb460e8d985.png)
ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_point()
![](https://img.haomeiwen.com/i25702203/2fa7ddea8cd289a5.png)
ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_jitter()
![](https://img.haomeiwen.com/i25702203/8d5dc2bc90041e29.png)
![](https://img.haomeiwen.com/i25702203/66a9f4ee44c555a8.png)
# 6.2堆叠直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))
![](https://img.haomeiwen.com/i25702203/896c6a938d4ad8ee.png)
# 6.3 并列直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
![](https://img.haomeiwen.com/i25702203/07ac6213e51849c6.png)
7.坐标系
#翻转coord_flip()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
![](https://img.haomeiwen.com/i25702203/51af1b304e5dbcc2.png)
#练习题走一波
ggplot(iris,aes(Species,Sepal.Width))+
geom_violin(aes(fill=Species))+
geom_boxplot()+
geom_jitter(aes(shape=Species))+
coord_flip()
![](https://img.haomeiwen.com/i25702203/80e8fe444482c6b4.png)
ggplot(iris,aes(Species,Sepal.Width))+
geom_violin(aes(fill=Species))+
geom_boxplot()+
geom_jitter(aes(shape=Species))+
coord_flip()+
theme_classic() # theme_classic()设置背景主题为白色不要网格
![](https://img.haomeiwen.com/i25702203/185670545110a8ac.png)
#极坐标系coord_polar()
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar + coord_flip()
bar + coord_polar()
![](https://img.haomeiwen.com/i25702203/d293b05288974171.png)
①ggplot2绘制点图和线图
#加载R包和Boston数据
library(tidyverse)
library(MASS)
data(Boston)
head(Boston)
![](https://img.haomeiwen.com/i25702203/2b41b9bbd9a15231.png)
#载入数据,aes映射数据到基本几何对象:点(x,y),size,shape,colour
p<-ggplot(data=Boston,aes(x=lstat,y=medv,colour=factor(rad)))
p
![](https://img.haomeiwen.com/i25702203/29f8ebb0ecdbc16a.png)
绘制几何对象:点,线图
p+geom_point(aes(colour=factor(rad)),size=2.5)
#colour=factor(rad)按类别分颜色
![](https://img.haomeiwen.com/i25702203/254bc5e1a9f3605c.png)
p+geom_point(colour="red",size=2.5)
![](https://img.haomeiwen.com/i25702203/5ece4c9aad3debca.png)
p+geom_point(aes(size=rad,shape=factor(rad)))
![](https://img.haomeiwen.com/i25702203/9adbacb8eacc825a.png)
Warning messages:
1: The shape palette can deal with a maximum of 6 discrete values
because more than 6 becomes difficult to discriminate; you
have 9. Consider specifying shapes manually if you must have
them.
2: Removed 173 rows containing missing values (geom_point).
解决上图问题用以下代码
p+geom_point(aes(size=rad,colour=factor(rad),shape=factor(rad)))+
scale_shape_manual(values=1:9)
![](https://img.haomeiwen.com/i25702203/65c58782ad3d88f7.png)
p+geom_line(colour='red')
![](https://img.haomeiwen.com/i25702203/1443ccf68b400f62.png)
②ggplot2绘制直方图
#R内置数据集mpg
p2<-ggplot(mpg,aes(hwy))
![](https://img.haomeiwen.com/i25702203/1540a0f657c0de8f.png)
p2+ geom_histogram(position ='identity', #position ='identity'两个直方图的位置重叠到一起
alpha=0.7,binwidth=2, #alpha=0.7填充颜色的透明度,binwidth=2直方图条形宽度
aes(y = ..density.., #y = ..density..y轴值取密度
fill =factor(year)))
![](https://img.haomeiwen.com/i25702203/6cf52408ba1ea7c3.png)
p2+ geom_histogram(position ='stack', #position ='stack'两个直方图的位置堆叠到一起
alpha=0.7,binwidth=2, #alpha=0.7填充颜色的透明度,binwidth=2直方图条形宽度
aes(y = ..density.., #y = ..density..y轴值取密度
fill =factor(year)))
![](https://img.haomeiwen.com/i25702203/3c618141663fd33e.png)
添加密度曲线
p2+ geom_histogram(position ='identity', #position ='identity'两个直方图的位置重叠到一起
alpha=0.7,binwidth=2, #alpha=0.7填充颜色的透明度,binwidth=2直方图条形宽度
aes(y = ..density.., #y = ..density..y轴值取密度
fill =factor(year)))+ #fill =factor(year)按照不同年份填充不同颜色
stat_density(geom ='line', position ='identity', #stat_density绘制密度曲线
aes(colour =factor(year)))
![](https://img.haomeiwen.com/i25702203/592059f0b8066b4f.png)
将一个图分成两个图
p2+ geom_histogram(position ='identity', #position ='identity'两个直方图的位置重叠到一起
alpha=0.7,binwidth=2, #alpha=0.7填充颜色的透明度,binwidth=2直方图条形宽度
aes(y = ..density.., #y = ..density..y轴值取密度
fill =factor(year)))+ #fill =factor(year)按照不同年份填充不同颜色
stat_density(geom ='line', position ='identity', #stat_density绘制密度曲线
aes(colour =factor(year)))+
facet_grid(.~year) #将一个图分成两个图
![](https://img.haomeiwen.com/i25702203/909f95605b17a41c.png)
网友评论