


基础包作图函数
plot(iris[,1],iris[,3],col = iris[,5])
text(6.5,4, labels = 'hello') #text中6.5和4为hello的坐标

boxplot(iris[,1]~iris[,5])

关闭画板
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))

2.映射-颜色、大小、透明度、形状

ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))


3.分面
ggplot(data = test) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species)

#双分面
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)

练习走一波
# 示例数据:ggplot2中数据集mpg
View(mpg)

# 1.分别以mpg的displ和hwy两列作为横纵坐标,画点图。
ggplot(data = mpg)+
geom_point(aes(x=displ,y=hwy))
#或者用下列代码
ggplot(data = mpg,aes(x=displ,y=hwy))+
geom_point()

# 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()

# 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)

# 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)

4.几何对象

ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))

ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length,
group = Species))

ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))


#局部映射
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))

#全局映射
ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
geom_smooth()+
geom_point()

# 1.尝试写出下图的代码
# 数据是iris
# X轴是Species
# y轴是Sepal.Width
# 图是箱线图
ggplot(iris,aes(x=Species,y=Sepal.Width))+
geom_boxplot()

# 2. 尝试在此图上叠加点图,
# 能发现什么问题?
ggplot(iris,aes(x=Species,y=Sepal.Width))+
geom_boxplot()+
geom_point()

# 3.用下列代码作图,观察结果
ggplot(test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
geom_point()+
geom_smooth(color = "black")

当局部映射和全局映射冲突,以局部映射为先。
5.统计变换
View(diamonds)


ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))

ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))

#统计变换使用场景
#5.1.不统计,数据直接做图
fre = as.data.frame(table(diamonds$cut))
fre

#使用表中数据直接作图而不统计
ggplot(data = fre) +
geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity") #stat = "identity"设定为不统计

#5.2count(计数)改为prop(百分比)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

6.位置调整
View(mpg)

ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_point()

ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_jitter()


# 6.2堆叠直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))

# 6.3 并列直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")

7.坐标系
#翻转coord_flip()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()

#练习题走一波
ggplot(iris,aes(Species,Sepal.Width))+
geom_violin(aes(fill=Species))+
geom_boxplot()+
geom_jitter(aes(shape=Species))+
coord_flip()

ggplot(iris,aes(Species,Sepal.Width))+
geom_violin(aes(fill=Species))+
geom_boxplot()+
geom_jitter(aes(shape=Species))+
coord_flip()+
theme_classic() # theme_classic()设置背景主题为白色不要网格

#极坐标系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()

①ggplot2绘制点图和线图
#加载R包和Boston数据
library(tidyverse)
library(MASS)
data(Boston)
head(Boston)

#载入数据,aes映射数据到基本几何对象:点(x,y),size,shape,colour
p<-ggplot(data=Boston,aes(x=lstat,y=medv,colour=factor(rad)))
p

绘制几何对象:点,线图
p+geom_point(aes(colour=factor(rad)),size=2.5)
#colour=factor(rad)按类别分颜色

p+geom_point(colour="red",size=2.5)

p+geom_point(aes(size=rad,shape=factor(rad)))

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)

p+geom_line(colour='red')

②ggplot2绘制直方图
#R内置数据集mpg
p2<-ggplot(mpg,aes(hwy))

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)))

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)))

添加密度曲线
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)))

将一个图分成两个图
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) #将一个图分成两个图

网友评论