7.ggplot2

作者: 萍智医信 | 来源:发表于2021-08-19 14:52 被阅读0次
常用绘图R包.png
基础包-绘图函数.png
基础包-绘图参数.png

基础包作图函数

plot(iris[,1],iris[,3],col = iris[,5]) 
text(6.5,4, labels = 'hello') #text中6.5和4为hello的坐标
image.png
boxplot(iris[,1]~iris[,5])
boxplot.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))
image.png

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


image.png
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))
image.png
手动设置.png

3.分面

ggplot(data = test) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species) 
单分面.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) 
双分面.png

练习走一波

# 示例数据:ggplot2中数据集mpg
View(mpg)
数据mpg.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()
image.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()
image.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)
image.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)
image.png

4.几何对象


image.png
ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))
image.png
ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                            y = Petal.Length,
                            group = Species))
image.png
ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length,
                          color = Species)) 
image.png
image.png
#局部映射
ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))+
  geom_point(mapping = aes(x = Sepal.Length, 
                           y = Petal.Length))
image.png
#全局映射
ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
  geom_smooth()+
  geom_point()
image.png
# 1.尝试写出下图的代码
# 数据是iris
# X轴是Species
# y轴是Sepal.Width
# 图是箱线图
ggplot(iris,aes(x=Species,y=Sepal.Width))+
  geom_boxplot()
image.png
# 2. 尝试在此图上叠加点图,
# 能发现什么问题?
ggplot(iris,aes(x=Species,y=Sepal.Width))+
  geom_boxplot()+
  geom_point()
image.png
# 3.用下列代码作图,观察结果
ggplot(test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
  geom_point()+
  geom_smooth(color = "black")
image.png

当局部映射和全局映射冲突,以局部映射为先。

5.统计变换

View(diamonds)
image.png
image.png
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut))
image.png
ggplot(data = diamonds) + 
  stat_count(mapping = aes(x = cut))
image.png
#统计变换使用场景
#5.1.不统计,数据直接做图
fre = as.data.frame(table(diamonds$cut))
fre
image.png
#使用表中数据直接作图而不统计
ggplot(data = fre) +
  geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity") #stat = "identity"设定为不统计
image.png
#5.2count(计数)改为prop(百分比)
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
image.png

6.位置调整

View(mpg)
mpg.png
ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_point()
image.png
ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_jitter()
image.png
diamonds.png
# 6.2堆叠直方图
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut,fill=clarity))
堆叠直方图.png
# 6.3 并列直方图
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
并列直方图.png

7.坐标系

#翻转coord_flip()

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
  geom_boxplot() +
  coord_flip()
image.png
#练习题走一波
ggplot(iris,aes(Species,Sepal.Width))+
  geom_violin(aes(fill=Species))+
  geom_boxplot()+
  geom_jitter(aes(shape=Species))+
  coord_flip()
image.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()设置背景主题为白色不要网格
image.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()
image.png

①ggplot2绘制点图和线图

#加载R包和Boston数据
library(tidyverse)
library(MASS)
data(Boston)
head(Boston)
Boston.png
#载入数据,aes映射数据到基本几何对象:点(x,y),size,shape,colour
p<-ggplot(data=Boston,aes(x=lstat,y=medv,colour=factor(rad)))
p
p.png

绘制几何对象:点,线图

p+geom_point(aes(colour=factor(rad)),size=2.5)
#colour=factor(rad)按类别分颜色
点.png
p+geom_point(colour="red",size=2.5)
点.png
p+geom_point(aes(size=rad,shape=factor(rad)))
点.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)
点.png
p+geom_line(colour='red')
线.png

②ggplot2绘制直方图

#R内置数据集mpg
p2<-ggplot(mpg,aes(hwy))
mpg.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)))
position ='identity'.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)))
position ='stack'.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)))
添加密度曲线.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)   #将一个图分成两个图
将一个图分成两个图.png

相关文章

  • 7.ggplot2

    基础包作图函数 ggplot2绘图 1.入门级绘图模板 2.映射-颜色、大小、透明度、形状 3.分面 练习走一波 ...

  • 7.ggplot2——网络图

    7.网络图 同地图的空间数据一样,网络图形在可视化领域占据了特殊部分,但空间数据在投影的使用上与常规绘图大不相同,...

网友评论

      本文标题:7.ggplot2

      本文链接:https://www.haomeiwen.com/subject/adoobltx.html