《ggplot2:数据分析与图形艺术》--学习笔记8

精雕细琢

主题

主题的控制包括标题、坐标轴标签、图例标签等文字调整,以及网格线、背景、轴须得颜色搭配。
两个内置主题theme_grey()theme_bw(),分别为灰底白网格线和白底灰网格线。
唯一参数base_size控制基础字体的大小。基础字体指做标题的大小,图形标题比它大20%,轴须标签比它小20%。

  • 全局性设置:theme_set(theme_grey())theme_set(theme_bw())theme_set()返回先前主题。
  • 局部性设置:只改变单个图形的主题,qplot()+theme_grey()
    主题元素
主题元素类型描述
aixs.linesegment直线和坐标轴
aixs.text.xtextx轴标签
aixs.text.ytexty轴标签
aixs.tickssegment轴须标签
aixs.title.xtext水平轴标题
aixs.title.ytext竖直轴标题
legend.backgroundrect图例背景
legend.keyrect图例符号
legend.texttext图例标签
legend.titletext图例标题
panel.backgroundrect面板背景
panel.borderrect面板边界
panel.grid.majorline主网格线
panel.grid.minorline次网格线
plot.titletext图形标题
strip.backgroundrect分面标签背景
strip.text.xtext水平条状文本
strip.text.ytext垂直条状文本
  • 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 设定图形存储的路径。
  • 三个控制输出尺寸的参数。空白,则使用当前屏幕图形设备尺寸。widthheight设置绝对尺寸大小,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()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Q_M_Y_Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值