精雕细琢
主题
主题的控制包括标题、坐标轴标签、图例标签等文字调整,以及网格线、背景、轴须得颜色搭配。
两个内置主题theme_grey()
和theme_bw()
,分别为灰底白网格线和白底灰网格线。
唯一参数base_size
控制基础字体的大小。基础字体指做标题的大小,图形标题比它大20%,轴须标签比它小20%。
- 全局性设置:
theme_set(theme_grey())
或theme_set(theme_bw())
,theme_set()
返回先前主题。 - 局部性设置:只改变单个图形的主题,
qplot()+theme_grey()
。
主题元素
主题元素 | 类型 | 描述 |
---|---|---|
aixs.line | segment | 直线和坐标轴 |
aixs.text.x | text | x轴标签 |
aixs.text.y | text | y轴标签 |
aixs.ticks | segment | 轴须标签 |
aixs.title.x | text | 水平轴标题 |
aixs.title.y | text | 竖直轴标题 |
legend.background | rect | 图例背景 |
legend.key | rect | 图例符号 |
legend.text | text | 图例标签 |
legend.title | text | 图例标题 |
panel.background | rect | 面板背景 |
panel.border | rect | 面板边界 |
panel.grid.major | line | 主网格线 |
panel.grid.minor | line | 次网格线 |
plot.title | text | 图形标题 |
strip.background | rect | 分面标签背景 |
strip.text.x | text | 水平条状文本 |
strip.text.y | text | 垂直条状文本 |
- element_text()绘制标签和标题,可控制字体的family, face,colour,size,hjust,vjust,angle,lineheight。当改变角度时,需将hjust调整至0或1。
library(ggplot2)
hgram <- qplot(psavert,data = economics,binwidth = 1)
previous_theme <- theme_set(theme_bw())
hgram + previous_theme
theme_set(previous_theme)
hgramt <- hgram + labs(title = "This is a histogram")
hgramt + theme(plot.title = element_text(size = 20))
hgramt + theme(plot.title = element_text(size = 20,colour = "red"))
hgramt + theme(plot.title = element_text(size = 20,hjust = 0.5))
hgramt + theme(plot.title = element_text(size = 20,hjust = 0.5,face = "bold"))
hgramt + theme(plot.title = element_text(size = 20,hjust = 0.5,angle = 180))
element_line()
绘制线条或线段,该元素函数可控制colour,size,linetype。
hgram + theme(panel.grid.major = element_line(colour = "red"))
hgram + theme(panel.grid.major = element_line(size=2))
hgram + theme(panel.grid.major = element_line(linetype = "dotted"))
hgram + theme(axis.line = element_line())
hgram + theme(axis.line = element_line(colour = "red"))
hgram + theme(axis.line = element_line(size = 0.5,linetype = "dashed"))
element_rect()
绘制主要供背景使用的矩形。可以控制填充颜色(fill)和边界的colour,size,linetype。
hgram + theme(plot.background = element_rect(fill = "grey80",colour = NA))
hgram + theme(plot.background = element_rect(size = 2))
hgram + theme(plot.background = element_rect(color = "red"))
hgram + theme(panel.background = element_rect())
hgram + theme(panel.background = element_rect(colour = NA))
hgram + theme(panel.background = element_rect(linetype = "dotted"))
element_blank()
空主题。
使用theme_get()
可以得到当前的主题设置。
theme_update()
可为后面图形的绘制进行全局性的修改。
old_theme <- theme_update(
plot.background = element_rect(fill = "#3366FF"),
panel.background = element_rect(fill = "#033DF5"),
axis.text.x = element_text(colour = "#CCFF33"),
axis.text.y = element_text(colour = "#CCFF33",hjust = 1),
axis.title.x = element_text(colour = "#CCFF33",face = "bold"),
axis.title.y = element_text(colour = "#CCFF33",face = "bold",
angle = 90)
)
qplot(cut,data = diamonds,geom = "bar")
qplot(cty,hwy,data = mpg)
theme_set(old_theme)
自定义标度和几何对象
几何对象和统计变换
类似update_geom_defaults()
和update_stat_defaults()
,我们也可以自定义几何对象和统计变换。只影响设置改变后新绘制的图形,而不是所有图形。
update_geom_defaults("point",aes(colour="darkblue"))
qplot(mpg,wt,data = mtcars)
update_stat_defaults("bin",aes(y=..density..))
qplot(cyl,data = mtcars,geom = "histogram",binwidth = 1)
存储输出
矢量和光栅图,矢量图是过程化的,意味着图形可无限缩放而没有细节的损失。光栅图以像素阵列形式存储,具有固定的最优观测大小。
可以用ggsave()
来存储图形,参数如下:
path
设定图形存储的路径。- 三个控制输出尺寸的参数。空白,则使用当前屏幕图形设备尺寸。
width
和height
设置绝对尺寸大小,scale
设置图形相对屏幕展示的尺寸大小。 - 对于光栅图形,dpi参数控制图形的分辨率,默认值300。
若你想将两幅图形存储到一个文件中,你需要打开基于磁盘的图形设备(比如png()或pdf()),打印图形,然后关闭图形设备dev.off()
。
qplot(mpg,wt,data = mtcars)
ggsave(file = "output.pdf")
pdf(file="output.pdf",width =6,height=6 )
#在脚本中,需要明确使用print()来打印图形
qplot(mpg,wt,data = mtcars)
qplot(wt,mpg,data = mtcars)
dev.off()
一页多图
关键概念:**视图窗口(viewport)**显示设备的一个矩形子区域。参数x,y,width,heigth控制视图窗口的大小和位置。模拟的测量单位是“npc”,范围从0到1。(0,0)代表的位置是左下角,(1,1)带表右上角,(0.5,0.5)代表视图窗口的中心。也可以用绝对单位如unit(2,"cm")
或unit(1,"inch")
。
首先绘制三幅子图。
a <- qplot(date,unemploy,data = economics,geom = "line")
a
b <- qplot(uempmed,unemploy,data = economics)+geom_smooth(se=F)
b
c <- qplot(uempmed,unemploy,data = economics,geom = "path")
c
library(grid)
##一个占据整个图形设备的视图窗口
vp1 <- viewport(width = 1,height = 1, x = 0.5, y= 0.5)
vp1 <- viewport()
## 只占了图形设备一半的宽和高的视图窗口,
## 定位在图形的中间位置
vp2 <- viewport(width = 0.5,height = 0.5,x=0.5,y=0.5)
vp2 <- viewport(width = 0.5,height = 0.5)
## 一个2cm x 3cm的视图窗口,定位在图形设备中心
vp3 <- viewport(width = unit(2,"cm"),height = unit(3,"cm"))
## 默认,x和y参数控制着视图窗口的中心位置,若想调整图形位置,需要通过just参数来控制将图形放置在哪个边角。
## 在右上角的视图窗口
vp4 <- viewport(x = 1,y = 1,just = c("right","top"))
## 处在左下角
vp5 <- viewport(x=0,y=0,just = c("right","bottom"))
# 为了在新的视图窗口中画图,还需要使用print()中的vp参数。
pdf("polishing-subplot-1.pdf",width = 4,height = 4) #绝对尺寸
subvp <- viewport(width = 0.4,height = 0.4,x=0.75,y=0.35)#相对尺寸
b#pdf中先放一个b
print(c,vp=subvp)#再在子视图窗口中绘制c
dev.off()
grid.newpage()
b
print(c,vp=subvp)
## 微调,文本应该更小,移除轴标签。
csmall <- c +
theme_gray(9) +
labs(x=NULL,y=NULL) +
theme(plot.margin = unit(rep(0,4),"lines"))
pdf("polishing-subplot-2.pdf",width = 4,height = 4)
b
print(csmall,vp=subvp)
dev.off()