ggplot2关于设置标签和注释

设置标签

在ggplot2包内,最主要用以设置标签的函数即geom_text()

一、设定常用设置

1.设定字体

字体的设置利用的是family参数,其中可以选择sans(黑体)、serif(宋体)、mono(等宽字体)

> rm(list = ls())
> #定义数据框
> df <- data.frame(x=1,y=3:1,
+                  family=c('sans','serif','mono'))
> ggplot(df,aes(x,y))+
+   geom_text(aes(label=family,family=family))

在这里插入图片描述

2.设定字体风格

字体风格分为默认(plain)、粗体(bold)和斜体(italic),而字体风格由fontface参数来控制

> #定义数据框
> df <- data.frame(x=1,y=3:1,
+                  face=c('plain','bold','italic'))
> ggplot(df,aes(x,y))+
+   geom_text(aes(label=face,fontface=face))

在这里插入图片描述

3.设定字体位置

字体的对齐方式,由hjust和vjust来设置
hjust参数可以设置为'left','center','right','inward'(文字向图中央对齐)
vjust参数可以设置为'bottom','middle','top','inward','outward'

> df <- data.frame(x=c(1,2,1,2,1.5),y=c(1,1,2,2,1.5),
+                  text=c('bottom-left','bottom-right','top-left',
+                         'top-right','center')
+                  )

在这里插入图片描述

> ggplot(df,aes(x,y))+
+   geom_text(aes(label=text))
> ggplot(df,aes(x,y))+
+   geom_text(aes(label=text),
+             vjust='inward',hjust='inward')

在这里插入图片描述size参数可以调整字体大小,angle参数可以定义文本的旋转角度
nudge_xnudge_y参数可以设置标签出现在点的附近,正值为右和上,负值相反

> df <- data.frame(trt=c('a','b','c'),resp=c(1.2,3.4,2.5))
> ggplot(df,aes(resp,trt)) +
+   geom_point() +
+   geom_text(aes(label=paste0('(',resp,')')),
+             nudge_y = -0.25) +
+               xlim(1,3.6)

在这里插入图片描述

二、标签优化

1.标签过多

当标签过多时,常常会产生无法繁杂的图,让人无法清晰读懂其中的信息

> ggplot(mpg,aes(displ,hwy)) +
+   geom_text(aes(label=model)) +
+   xlim(1,8)

在这里插入图片描述
同时设置check_overlap = TRUE可以将重叠的标签删除

> ggplot(mpg,aes(displ,hwy)) +
+   geom_text(aes(label=model),check_overlap = TRUE) +
+   xlim(1,8)

在这里插入图片描述
但如果同时画出点后,就显得有些乱

> ggplot(mpg,aes(displ,hwy)) +
+   geom_point(colour='red') +
+   geom_text(aes(label=model),check_overlap = TRUE) +
+   xlim(1,8)

在这里插入图片描述

2.用注释代替图例

directlabels包提供了一系列标签定位方法
绘制点图可以使用smart.grid,而如果是线图则可以指定last.polygonslast.qp

> ggplot(mpg,aes(displ,hwy,colour=class)) +
+   geom_point(show.legend = FALSE) +#隐藏图例
+   directlabels::geom_dl(aes(label=class),method='smart.grid')

在这里插入图片描述

3.指定点标签

如果说需要对制造商为toyota的数据点进行标签的设置,只需要在geom_point()内部设定具体的数据库就可以了

#指定点注释
p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(
    data = filter(mpg, manufacturer == "toyota"), 
    colour = "orange",
    size = 3
  ) +
  geom_point() 
p

在这里插入图片描述
但可以看出,因为是标签的原因并没有显示出图例,所以可以利用annotate()函数进行补充

> p + 
+   annotate(geom = "point", x = 5.5, y = 40, colour = "orange", 
+ size = 3) +
+   annotate(geom = "point", x = 5.5, y = 40) + 
+   annotate(geom = "text", x = 5.6, y = 40, label = "toyota", hjust = "left")

在这里插入图片描述分别设置点、颜色以及标签于具体的坐标上

设置注释

注释可以为特定的数据点添加标签,在面对表示离群点或其他重要点的情况中尤为有效
geom_rect()可以标注图形中特定的矩阵部分,其中包括了xmin、xmax、ymin、ymax参数。
而geom_vline()、geom_hline()以及geom_abline()函数可以在图形中添加全覆盖的参照线
利用的是economics数据集,首先简答查看一下数据库信息,并绘制线图

> head(economics)
# A tibble: 6 × 6
  date         pce    pop psavert uempmed unemploy
  <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
1 1967-07-01  507. 198712    12.6     4.5     2944
2 1967-08-01  510. 198911    12.6     4.7     2945
3 1967-09-01  516. 199113    11.9     4.6     2958
4 1967-10-01  512. 199311    12.9     4.9     3143
5 1967-11-01  517. 199498    12.8     4.7     3066
6 1967-12-01  525. 199657    11.8     4.8     3018
> ggplot(economics,aes(date,unemploy)) + geom_line()

在这里插入图片描述这里提取各个美国总统作为划分的依据

> presidential <- subset(presidential, start > economics$date[1])

绘制图形,其中所使用的-Inf和Inf分别代表注释的位置,上和下或者左和右
利用geom_rect()函数进行图形分割,利用了上述各个总统进行分割,利用其中fill参数进行填充,此处利用两个党派进行颜色填充,并设置竖直参照线

> p <- ggplot(economics) + 
+   geom_rect(
+     aes(xmin = start, xmax = end, fill = party), 
+     ymin = -Inf, ymax = Inf, alpha = 0.2, 
+     data = presidential
+   ) + 
+   geom_vline(
+     aes(xintercept = as.numeric(start)), 
+     data = presidential,
+     colour = "grey50", alpha = 0.5
+   )

> p

在这里插入图片描述

接着设定注释位置,aes内设定具体坐标位置

> p + 
+   geom_text(aes(x = start, y = 2500, label = name), 
+     data = presidential, 
+     size = 3, vjust = 0, hjust = 0, nudge_x = 50) 
+   geom_line(aes(date, unemploy))
> p

在这里插入图片描述

最后设定填充颜色

> p + 
  scale_fill_manual(values = c("blue", "red"))
> p

在这里插入图片描述

在分面内使用参照线

在通过分面比较组间关系时,利用参照线可以更清晰看出数据间的差别
其中geom_bin2d()可以设置二维密图,也称为二维直方图,其中的bins参数可以设置组数

> # 在分面内使用参照线
> ggplot(diamonds,aes(log10(carat),log10(price))) +
+   geom_bin2d() +
+   facet_wrap(~cut,nrow = 1)

在这里插入图片描述

接着构造一个函数,并提取直线方程

> mod_coef <- coef(lm(log10(price)~log10(carat),data = diamonds))
> mod_coef
 (Intercept) log10(carat) 
    3.669207     1.675817 

接着利用geom_abline()函数进行函数插入,内部的intercept参数代表截距,slope参数代表斜率

> ggplot(diamonds,aes(log10(carat),log10(price))) +
+   geom_bin2d() + 
+   geom_abline(intercept = mod_coef[1],slope = mod_coef[2],
+               colour='white',size=1) +
+   facet_wrap(~cut,nrow = 2)

在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值