ggplot2_总纲

时间:2022-07-25
本文章向大家介绍ggplot2_总纲,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

ggplot2_概述

sunqi

2020/7/31

概述

年初的时候我好像打算对ggplot2进行一个教程,后来因为其他事情耽搁了,今天打开以往的git日志,才发现有这么一个坑(ggplot2初探),虽然现在绘图的包层出不穷,但是ggplot真的是一个基础的绘图包了。

ggplot2 介绍

语法构成

GGPlot2是一个强大而灵活的R包,由HadleyWickham实现, ggplot2中的gg表示Grammar of graphics,ggplot通过使用“语法”来描述图形,基本部分为: plot =data+Aesthetics+Geometry:

data: a data frame aesthetics: 用于表示x和y变量。它也可以用来控制颜色,大小和形状的点,等等 geometry: 几何:对应于图形的类型(柱状图,盒状图,线状图,…)

主要的函数

Plot types

GGPlot2 functions

Initialize a ggplot

ggplot()

Scatter plot

geom_point()

Box plot

geom_boxplot()

Violin plot

geom_violin()

strip chart

geom_jitter()

Dot plot

geom_dotplot()

Bar chart

geom_bar() or geom_col()

Line plot

geom_line()

Histogram

geom_histogram()

Density plot

geom_density()

Error bars

geom_errorbar()

QQ plot

stat_qq()

ECDF plot

stat_ecdf()

Title and axis labels

labs()

例子

对iris数据集进行绘制散点图

library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
  geom_point()
# 更改点的大小颜色形状
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
  geom_point(size = 1.2, color = "steelblue", shape = 21)
# 添加分组,这里在geom_point中使用aes
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
  geom_point(aes(color = Species, shape = Species))
# Change the default color manually.
# 使用scale_color_manual() 函数更改配色方案
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
  geom_point(aes(color = Species, shape = Species))+
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
print(p)
# 更改图例的位置theme
# 上下左右位置left”, “top”, “right”, “bottom”, “none”
p + theme(legend.position = "top")
# 标题和轴标签labs()
p + labs(
  title = "Edgar Anderson's Iris Data",
  subtitle = "iris is a data frame with 150 cases (rows) and 5 variables",
  x = "Sepal Length (cm)", y = "Sepal Width (cm)"
  )
# 分面问题Facet
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
  # 点图分组
  geom_point(aes(color = Species))+
  # 添加光滑的曲线和置信区间
  geom_smooth(aes(color = Species, fill = Species))+
  # 使用facet_wrap函数对数据分面展示
  facet_wrap(~Species, ncol = 3, nrow = 1)+
  # 添加配色方案
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))+
  scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
# ggplot theme
# ggplot的默认主题是theme gray(),它是具有灰色背景和白色网格线的主题。
# 更多的主题包括:theme bw()、theme classic()和theme minimal()
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
  geom_point()+
  theme_classic()

图片的保存

ggplot标准绘图流程

  1. 打开绘图设置

pdf(“r-graphics.pdf”) svg(“r-graphics.svg”) png(“r-graphics.png”) tiff(“r-graphics.tiff”) jpeg(“r-graphics.jpg”)

  1. 绘图
  2. 关闭绘图

dev.off()

例子

导出绘图到pdf文件

# 绘图
library(ggplot2)
myplot1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point()
myplot2 <- ggplot(iris, aes(Species, Sepal.Length)) +
  geom_boxplot()

# 保存图片到pdf
pdf("ggplot.pdf")
print(myplot1)     # Plot 1 --保存到第一页
print(myplot2)     # Plot 2 ---保存到第二页
dev.off()
## png
##   2
# 保存png格式
png("myplot.png")
print(myplot1)
dev.off()
## png
##   2
# 绝大多数的时候我用的是ggsave函数

# 1. 建立图形文件
ggplot(mtcars, aes(wt, mpg)) + geom_point()
# 2.1. 保存pdf,这里没有指定保存图形,一般为现在绘图面板中的图形
ggsave("myplot.pdf")
# 2.2 OR save it to png file
ggsave("myplot.png")

结束语

今天是公众号创立以来的第100篇文章,转眼间已经过去这么长时间了,物是人非,再回首,有些人背叛了革命,有些人迷失在了熙熙攘攘的人海,总归是意难平。

未来,希望能够继续完善这个公号,不负年华。

最后,不要温柔走入那良夜。

love&peace