前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用element_text在ggplot2中自定义文本

使用element_text在ggplot2中自定义文本

作者头像
前端小tips
发布2021-12-06 13:42:12
2.4K0
发布2021-12-06 13:42:12
举报
文章被收录于专栏:前端文章小tips

ggplot2的主题系统可以让我们更好的控制图形 非数据元素 的细节,通过更加精细的修改来提升图像的美感,ggplot2 的主题系统自带多个 element_ 功能

  • element_text( )
  • element_line( )
  • element_rect( )
  • element_blank( )

本节来介绍主题元素element_text() ,使用它控制绘图中文本元素的许多部分,如字体大小、颜色和字体类型。

ggplot2的element_text()剖析
element_text() 控制的元素列表
  • axis.title.x: 自定义 x 轴标签/标题
  • axis.title.y : 自定义 y 轴标签/标题
  • axis.text.x : 自定义 x 轴刻度标签
  • axis.text.y : 自定义 y 轴刻度标签
  • legend.title: 自定义图例标题文本
  • legend.text:自定义图例文本
  • plot.title: 自定义图像主标题
  • plot.subtitle: 自定义图像副标题
  • plot.caption: 自定义图像的脚注
  • plot.tag: 自定义绘图的标签
加载R包
代码语言:javascript
复制
library(tidyverse)
library(palmerpenguins)

依旧还是使用企鹅的数据集,接下来使用element_text() 函数来调整图像的文本元素

代码语言:javascript
复制
p<- penguins %>%
  drop_na() %>%
  ggplot(aes(x=flipper_length_mm,
             y=bill_length_mm, 
             color=species,
             shape=sex))+
  geom_point()+
  labs(title="Palmer Penguins",
       subtitle="Flipper Length vs Bill Length",
       caption="cmdlinetips.com",
       tag = 'A'
  )
​
p
1. axis.title.*( ):自定义x&y标签文本

通过element_text( )来更改文本,颜色,大小和角度

代码语言:javascript
复制
p + theme(axis.title.x = element_text(size=16, color="purple", 
                                      face="bold",angle=0),
          axis.title.y = element_text(size=16, color="purple", 
                                      face="bold",angle=90))
2. axis.text.*( )自定义x&y刻度文本
代码语言:javascript
复制
p + theme(axis.text.x=element_text(family = "Tahoma",face="bold",
                                   colour="black",size=10),
          axis.text.y = element_text(family = "Tahoma",face="bold",
                                     colour="black",size=10))
3. legend.title( )自定义图例标题文本
代码语言:javascript
复制
p + theme(legend.title=element_text(color="purple",
                                    face="bold",size=12))
4. legend.text( )自定义图例文本
代码语言:javascript
复制
p + theme(legend.text=element_text(face="bold", color="red",size=10))
5. plot.title( ) 自定义主标题
代码语言:javascript
复制
p + theme(plot.title= element_text(size=15,color="blue",hjust = 0.5,
                                   face="bold",family = "Tahoma"))
6. plot.subtitle( )自定义副标题
代码语言:javascript
复制
p + theme(plot.subtitle= element_text(size=13,
                                      color="red",
                                      face="bold"))
7. plot.caption( )自定义脚注
代码语言:javascript
复制
p + theme(plot.caption= element_text(size=12,
                                     color="blue",
                                     face="bold"))
8. plot.tag( )自定义标签
代码语言:javascript
复制
p + theme(plot.tag = element_text(size=16,
                                   color="red",
                                   face="bold"))

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • ggplot2的element_text()剖析
  • element_text() 控制的元素列表
  • 加载R包
  • 1. axis.title.*( ):自定义x&y标签文本
  • 2. axis.text.*( )自定义x&y刻度文本
  • 3. legend.title( )自定义图例标题文本
  • 4. legend.text( )自定义图例文本
  • 5. plot.title( ) 自定义主标题
  • 6. plot.subtitle( )自定义副标题
  • 7. plot.caption( )自定义脚注
  • 8. plot.tag( )自定义标签
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档