library
(ggplot2)
library
(grid)
grid.newpage
()
# 创建一个空白画布
vp1 <-
viewport
(
x =
0.5
,
y =
0.5
,
width =
0.5
,
height =
0.25
,
angle =
45
)
# 设定视窗大小
grid.show.viewport
(vp1)
# 查看在画布中的的视窗,自动上色,方便观察
library
(ggplot2)
library
(grid)
library
(showtext) YaHei <-
windowsFont
(
"微软雅黑"
)
font_add
(
"YaHei"
,
regular =
"msyh.ttc"
,
bold =
"msyhbd.ttc"
)
# 右键字体,然后点击属性,regular指定常规, bold表示指定粗体字体
showtext_auto
() #### 父viewport
grid.newpage
()
#新建一个page
vp1 <-
viewport
(
x =
0
,
y =
0.2
,
w =
0.9
,
h =
0.8
,
just =
c
(
"left"
,
"bottom"
))
#新建一个viewport,起点为左下角,
pushViewport
(vp1)
# 推出vp1
grid.rect
(
gp =
gpar
(
col =
"red"
))
# 新建一个矩形,gp=gpar()表示设置图形参数
grid.text
(
"我在vp1_1"
,
x =
0.8
,
y =
0.2
,
gp =
gpar
(
col =
"red"
,
fontfamily =
"YaHei"
,
fontsize =
15
))
# 新建一个文本,输出到vp1
vp2 <-
viewport
(
x =
0
,
y =
0.2
,
w =
0.9
,
h =
0.8
,
just =
c
(
"left"
,
"bottom"
))
# 新建一个viewport,起点为左下角,
pushViewport
(
name =
vp2)
# 将工作区域设置到vp2
grid.rect
(
x =
0.1
,
y =
0.2
,
width =
0.9
,
height =
0.7
,
just =
c
(
"left"
,
"bottom"
),
gp =
gpar
(
col =
"blue"
,
lty =
"dashed"
))
# 新建一个矩形,gp=gpar()表示设置图形参数
grid.text
(
"我在vp2_1"
,
x =
0.8
,
y =
0.3
,
gp =
gpar
(
col =
"blue"
,
fontfamily =
"YaHei"
,
fontsize =
15
))
# 新建一个文本,输出到vp2
vp3 <-
viewport
(
x =
0.1
,
y =
0.2
,
width =
0.9
,
height =
0.7
,
just =
c
(
"left"
,
"bottom"
))
pushViewport
(vp3)
grid.rect
(
x =
0.1
,
y =
0.2
,
width =
0.7
,
height =
0.7
,
just =
c
(
"left"
,
"bottom"
),
gp =
gpar
(
col =
"orange"
,
lty =
"twodash"
,
lwd =
2
))
# 新建一个矩形,gp=gpar()表示设置图形参数
grid.text
(
"我在vp3_1"
,
x =
0.6
,
y =
0.4
,
gp =
gpar
(
col =
"orange"
,
fontfamily =
"YaHei"
,
fontsize =
15
))
# 新建一个文本,输出到vp2
layout参数
library
(ggplot2)
library
(grid)
grid.newpage
() g1 <-
grid.layout
(
nrow =
3
,
ncol =
2
,
widths =
c
(
2
,
3
),
heights =
c
(
2
,
1
,
3
))
# 设置分割的宽度和长度比例
grid.show.layout
(
l =
g1)
综合例子
library
(ggplot2)
library
(grid)
library
(showtext) YaHei <-
windowsFont
(
"微软雅黑"
)
font_add
(
"YaHei"
,
regular =
"msyh.ttc"
,
bold =
"msyhbd.ttc"
)
# 右键字体,然后点击属性,regular指定常规, bold表示指定粗体字体
showtext_auto
() plot.iris <-
ggplot
(iris,
aes
(Sepal.Length, Sepal.Width))
+
geom_point
()
+
facet_grid
(
cols =
vars
(Species))
# 按Species列分面
plot.mpg <-
ggplot
(mpg,
aes
(
x =
cty,
y =
hwy,
colour =
factor
(cyl)))
+
geom_point
(
size =
2.5
)
+
labs
(
title =
"dot plot"
) plot.diamonds <-
ggplot
(diamonds,
aes
(clarity,
fill =
cut))
+
geom_bar
()
+
theme
(
axis.text.x =
element_text
(
angle =
70
,
vjust =
0.5
))
+
labs
(
title =
"bar plot"
)
grid.newpage
()
# 新建画布
layout_
1
<-
grid.layout
(
nrow =
3
,
ncol =
2
,
widths =
c
(
1
,
1
),
heights =
c
(
1
,
4
,
5
))
# 分成上下2*3共6个版块,最上面版块显示标题
pushViewport
(
viewport
(
layout =
layout_
1
))
# 推出分成6个版块的视窗
print
(plot.iris,
vp =
viewport
(
layout.pos.row =
2
,
layout.pos.col =
c
(
1
,
2
)))
# 在中间一行子视窗中画plot.iris
print
(plot.mpg,
vp =
viewport
(
layout.pos.row =
3
,
layout.pos.col =
1
))
# 在左下角子视窗中画plot.mpg
print
(plot.diamonds,
vp =
viewport
(
layout.pos.row =
3
,
layout.pos.col =
2
))
#在右下角子视窗中画plot.diamonds
grid.text
(
"我是画布名称"
,
x =
0.5
,
y =
0.95
,
gp =
gpar
(
col =
"orange"
,
fontfamily =
"YaHei"
,
fontsize =
15
))
# 增加画布标题
3.1
子母图
字母图,主要是形成局部放大的效果,既可以从整体上对比,又兼顾特别小的数据组,或特别密的数据点可以查看,而没有必要单独做2张图
library
(ggplot2)
library
(grid)
3.2
grid 拼接蝴蝶图
library
(ggplot2)
library
(grid)
library
(dplyr)
library
(showtext)
library
(Cairo) YaHei <-
windowsFont
(
"微软雅黑"
)
font_add
(
"YaHei"
,
regular =
"msyh.ttc"
,
bold =
"msyhbd.ttc"
)
# 右键字体,然后点击属性,regular指定常规, bold表示指定粗体字体
CairoPNG
(
file =
"E:/R_input&output/images_output/蝴蝶图_exercing.png"
,
width =
1200
,
height =
700
)
showtext_begin
()
#生成图形所需数据集:
mydata<-
data.frame
(
id=
1
:
14
,
A=
c
(
5.0
,
14.7
,
2.5
,
8.5
,
5.1
,
6.9
,
7.7
,
6.8
,
4.4
,
4.9
,
5.3
,
1.0
,
0.9
,
7.8
),
B=
c
(
31.3
,
24.7
,
17.8
,
17.2
,
15.3
,
14.3
,
13.9
,
13.9
,
12.4
,
10.0
,
6.5
,
4.2
,
2.5
,
0.9
),
Label=
c
(
"Website"
,
"Customer & Employee Referral"
,
"Webinar"
,
"Facebook/Twitter/Other Social"
,
"Marketting & Advertising"
,
"Paid Serch"
,
"Other"
,
"Sales generated"
,
"Tradeshows"
,
"Parter"
,
"Linkedin"
,
"Events"
,
"Lead list"
,
"Emial Campaign"
)) p1<-
ggplot
(mydata)
+
# 绘制右侧的柱形图
geom_hline
(
yintercept=
mean
(mydata
$
A),
linetype=
2
,
size=
.
25
,
colour=
"grey"
)
+
geom_bar
(
aes
(
x=
id,
y=
A),
stat=
"identity"
,
fill=
"#E2BB1E"
,
colour=
NA
)
+
ylim
(
-
5.5
,
16
)
+
scale_x_reverse
()
+
geom_text
(
aes
(
x=
id,
y=
-
4
,
label=
Label),
vjust=
.
5
)
+
geom_text
(
aes
(
x=
id,
y=
A
+
.
75
,
label=
paste0
(A,
"%"
)),
size=
4.5
,
family=
"YaHei"
,
fontface=
"bold"
)
+
coord_flip
()
+
theme_void
() p1
p2<-
ggplot
(mydata)
+
# 绘制左侧柱形图, 左侧图没有横坐标刻度标签
geom_hline
(
yintercept=
-
mean
(mydata
$
B),
linetype=
2
,
size=
.
25
,
colour=
"grey"
)
+
geom_bar
(
aes
(
x=
id,
y=
-
B),
stat=
"identity"
,
fill=
"#C44E4C"
,
colour=
NA
)
+
# y=-B,绘制的图形在另一侧
ylim
(
-
40
,
0
)
+
scale_x_reverse
()
+
#
geom_text
(
aes
(
x=
id,
y=
-
B
-
1.75
,
label=
paste0
(B,
"%"
)),
size=
4.5
,
family=
"YaHei"
,
fontface=
"bold"
)
+
coord_flip
()
+
theme_void
() p2
# 图形拼接
grid.newpage
()
# 新建画布
layout_
1
<-
grid.layout
(
nrow =
2
,
ncol =
2
,
widths =
c
(
2
,
3
),
heights =
c
(
1
,
9
))
# 分成2*2共4个版块
pushViewport
(
viewport
(
layout =
layout_
1
))
# 推出分为4个版块的视窗
print
(p1,
vp =
viewport
(
layout.pos.row =
2
,
layout.pos.col =
2
))
# 将p1输出到右下角
print
(p2,
vp =
viewport
(
layout.pos.row =
2
,
layout.pos.col =
1
))
# 将p2输出到左下角# 添加主标题和分标题
grid.text
(
label=
"我是主标题"
,
x =
0.5
,
y =
0.97
,
gp=
gpar
(
col=
"cyan"
,
fontsize=
15
,
fontfamily=
"YaHei"
,
draw=
TRUE
,
just =
"centre"
))
grid.text
(
label=
"我是左标题"
,
x =
0.15
,
y =
0.94
,
gp=
gpar
(
col=
"blue"
,
fontsize=
10
,
fontfamily=
"YaHei"
,
draw=
TRUE
,
just =
c
(
"left"
,
"top"
)))
grid.text
(
label=
"我是右标题"
,
x =
0.85
,
y =
0.94
,
gp=
gpar
(
col=
"blue"
,
fontsize=
10
,
fontfamily=
"YaHei"
,
draw=
TRUE
,
just =
c
(
"right"
,
"top"
)))
showtext_end
()
dev.off
()
## png ## 2
蝴蝶图
4
gridExtra包
主要函数:
arrangeGrob()
grid.arrange()
marrangeGrob()
语法:
arrangeGrob(..., grobs = list(...), layout_matrix, vp = NULL, name = "arrange", as.table = TRUE, respect = FALSE, clip = "off", nrow = NULL, ncol = NULL, widths = NULL, heights = NULL, top = NULL, bottom = NULL, left = NULL, right = NULL, padding = unit(0.5, "line")) grid.arrange(..., newpage = TRUE) marrangeGrob(grobs, ..., ncol, nrow, layout_matrix = matrix(seq_len(nrow * ncol), nrow = nrow, ncol = ncol), top = quote(paste("page", g, "of", npages)))
参数解释:
grobs 图形对象列表,grob是graphical object两个单词的缩写,表示ggpot等图形对象
layout_matrix 表示布局的矩阵
library
(ggplot2)
library
(gridExtra)
library
(showtext) YaHei <-
windowsFont
(
"微软雅黑"
)
font_add
(
"YaHei"
,
regular =
"msyh.ttc"
,
bold =
"msyhbd.ttc"
)
# 右键字体,然后点击属性,regular指定常规, bold表示指定粗体字体
showtext.auto
() empty <-
ggplot
()
+
geom_point
(
aes
(
1
,
1
),
colour =
"white"
)
+
theme
(
axis.ticks =
element_blank
(),
panel.background =
element_blank
(),
axis.line =
element_blank
(),
axis.text.x =
element_blank
(),
axis.text.y =
element_blank
(),
axis.title.x =
element_blank
(),
axis.title.y =
element_blank
()) scatter <-
ggplot
()
+
geom_point
(
aes
(mtcars
$
mpg, mtcars
$
qsec))
# 绘制主图散点图
hist_top <-
ggplot
()
+
geom_histogram
(
aes
(mtcars
$
mpg))
# 绘制上方频率分布直方图
hist_right <-
ggplot
()
+
geom_histogram
(
aes
(mtcars
$
qsec))
+
coord_flip
()
# 绘制右侧频率分布直方图# 最终组合,由4个图拼图而成,只有右上角的图已经将标注移除了
grid.arrange
(hist_top, empty, scatter, hist_right,
# 按从左到右,从上到下顺序排列4个图
ncol =
2
,
nrow =
2
,
widths =
c
(
4
,
1
),
heights =
c
(
1
,
4
))
# 4个版块的长宽比例# 其实这种组合图已经有相应的R包了,ggExtra# df <- data.frame(x = mtcars$mpg, y = mtcars$qsec)# p <- ggplot(df, aes(x, y)) + geom_point() + theme_classic()# ggExtra::ggMarginal(p, type = "histogram")
把绘图对象添加到列表总,并把该列表传递给
grid.arrange()
函数中的grobs参数
library
(ggplot2)
library
(gridExtra)
library
(lattice)
# 类似ggplot,但是语法更加复杂
library
(showtext) YaHei <-
windowsFont
(
"微软雅黑"
)
font_add
(
"YaHei"
,
regular =
"msyh.ttc"
,
bold =
"msyhbd.ttc"
)
# 右键字体,然后点击属性,regular指定常规, bold表示指定粗体字体
showtext.auto
() gs <-
list
(
NULL
) gs[[
1
]] <-
qplot
(
1
,
1
) gs[[
2
]] <-
xyplot
(
1
~
1
)
# lattice包
grid.arrange
(
grobs =
gs,
ncol =
2
)
子母图
grid包可以画字母图
安装gridExtra包后,ggplot2中多了一个
ggplotGrob(
)函数,可以创建grob对象参数
library
(ggplot2)
library
(gridExtra) g <-
ggplotGrob
(
qplot
(
1
,
1
)
+
theme
(
plot.background =
element_rect
(
colour =
"black"
)))
qplot
(
1
:
10
,
1
:
10
)
+
annotation_custom
(
grob =
g,
# 插入图形对象,即添加内容
xmin =
1
,
xmax =
5
,
ymin =
5
,
ymax =
10
# 添加位置4个坐标
)
版权声明
本文经授权转自EasyCharts公众号
。
文章转载只为学术新闻信息的传播,不代表我公众号所
持的观点。
联系我们
返回搜狐,查看更多