本文首发于“生信补给站”公众号,https://mp.weixin.qq.com/s/hMjPj18R1cKBt78w8UfhIw

学习了ggplot2的基本绘图元素ggplot2|详解八大基本绘图要素,可以初步绘制出需要展示的图形,legend可以对图例进行细节的修改ggplot2 |legend参数设置,图形精雕细琢,那theme有什么用呢?

theme是解决图是否美观的一个工具,其与scale最大的区别在于不受数据左右先把scale做好,就是一张合格的图;再处理theme,则是一张出色的图

一 载入数据,R包

library(ggplot2)
#载入数据
data(diamonds)
set.seed(1234)
diamond <- diamonds[sample(nrow(diamonds), 2000), ]

# 绘制初始图形
p <- ggplot(data = diamond) +geom_point(aes(x=carat, y=price, colour=color,shape=cut)) + labs(title="学习ggplot2可视化",subtitle = "参数好多学不会?",caption = "熟能生巧")
p

可以上图的标题,轴标签和图例已经默认设置好了,是否可以个性化修改呢? 当然可以!!!

输入?theme可以看到theme()功能的大量参数,可以实现与外观相关的大多数要求。其中有四种主要类型:

  1. element_text():使用element_text()函数设置基于文本的组件,如title,subtitle 和caption等。

  2. element_line():使用element_line()设置基于线的组件,如轴线,主网格线和次网格线等。

  3. element_rect():使用element_rect()修改基于矩形的组件,如绘图区域和面板区域的背景。

  4. element_blank():使用element_blank()关闭显示的主题内容。

二 精雕细琢

1 修改标题,坐标轴

由于绘图和轴标题是文本组件,使用element_text()参数修改。

设置title的尺寸,颜色,线高,位置

p + theme(plot.title=element_text(size=20, 
                                   face="bold",
                                   color="skyblue", #颜色
                                   hjust=0.5, #调整位置,正中间
                                   lineheight=1.2))

设置subtitle和caption

p + theme(plot.subtitle=element_text(size=15, 
                                      face="bold",
                                      color = "red",
                                      hjust=0.5),  # subtitle
           plot.caption=element_text(size=15))  # caption

修改坐标轴

p + theme(axis.title.x=element_text(vjust=1,  
                                     size=20),  # X axis title
           axis.title.y=element_text(size=10,
                                    color = "blue"),  # Y axis title
           axis.text.x=element_text(size=10,
                                    angle = 45,
                                    color = "red",
                                    vjust=.5),  # X axis text
           axis.text.y=element_text(size=10))  # Y axis text

以上示例涵盖了一些常用的主题修改,其中

  • vjust,控制标题(或标签)和绘图之间的垂直间距。

  • hjust,控制水平间距。将其设置为0.5将标题居中。

  • face,设置字体(“plain”,“italic”,“bold”,“bold.italic”)

2 修改图例

设置图例标题,文本和键的样式

图例的关键是像元素一样的图形,因此使用element_rect()函数进行设置。

p + theme(legend.title = element_text(size=15, color = "firebrick"), 
          legend.text = element_text(size=10),
          legend.key=element_rect(fill='green'))

删除图例和更改图例位置

图例是主题的一个方面,因此可以使用theme()功能进行修改。其中legend.justification参数可以将图例设置在图中,legend.position参数用来将图例设置在图表区域,其中x和y轴的位置(0,0)是在图表的左下和(1,1)是右上角。

# No legend --------------------------------------------------
p + theme(legend.position="None") + labs(subtitle="No Legend")

# legend at the bottom and horizontal ------------------------
p + theme(legend.position="bottom", legend.box = "horizontal") + labs(subtitle="Legend bottom")

# legend at bottom-right, inside the plot --------------------
p + theme(legend.title = element_text(size=12, color = "salmon", face="bold"),
          legend.justification=c(1,0),
          legend.position=c(0.95, 0.05),  
          legend.background = element_blank(),
          legend.key = element_blank()) +
 labs(subtitle="Legend: Bottom-Right Inside the Plot")

3 修改绘图背景,主轴和次轴

更改绘图背景

# 更改绘图背景和绘图区域
p + theme(panel.background = element_rect(fill = 'grey80'),
 plot.background=element_rect(fill="khaki"),
         plot.margin = unit(c(3, 2, 1, 1), "cm")) +  #设置绘图区域距离边的据类,上,右,下,左
   labs(title="Modified Background", subtitle="Change Plot Margin")  

更改主次网格线以及X,Y坐标轴

# Change Plot Background elements -----------------------------------
p + theme(
         panel.grid.major = element_line(colour = "burlywood", size=1.5),
         panel.grid.minor = element_line(colour = "tomato",
                                         size=0.25,
                                         linetype = "dashed"),
         panel.border = element_blank(),
         axis.line.x = element_line(colour = "darkorange",
                                    size=1.5,
                                    lineend = "butt"),
         axis.line.y = element_line(colour = "skyblue",
                                    size=1.5)) +
   labs(
        subtitle="Change Major and Minor grid, Axis Lines")

删除主,次网格线,边框,轴标题,文本和刻度

p + theme(panel.grid.major = element_blank(), #主网格线
         panel.grid.minor = element_blank(), #次网格线
         panel.border = element_blank(), #边框
         axis.title = element_blank(),  #轴标题
         axis.text = element_blank(), # 文本
         axis.ticks = element_blank()) +
 labs(title="Modified Background", subtitle="Remove major and minor axis grid, border, axis title, text and ticks")

默认主题以及自定义主题

ggplot2 自带主题

theme_grey()为默认主题,theme_bw()为白色背景主题,theme_classic()为经典主题。

p + theme_bw() +
   labs(subtitle="Change theme_bw")

ggplot2 扩展包主题

library(ggthemes)
p + theme_economist() +
  labs(subtitle="Change theme_economist")
#其他可选theme_economist theme_economist_whitetheme_wsj theme_exceltheme_few
#theme_foundationtheme_igray theme_solarizedtheme_stata theme_tufte

自定义主题

可根据常见需要自定义常用主题

p + theme_MJ() + labs(subtitle = "Change theme_MJ")

学习ggplot2的八大基本元素,legend的基本设置后,现在也清楚了主题的相关设置,先把scale做好,就是一张合格的图;再处理theme,则是一张出色的图

【关注“生信补给站”公众号,对话框回复 R-theme 即可获得上述R代码】

更多关于生信,R,Python的内容请扫码关注小号,谢谢。

ggplot2|theme主题设置,详解绘图优化-“精雕细琢”-的更多相关文章

  1. Hexo+NexT(三):Next主题配置详解

    阅读本篇之前,假定读者已经有了Node.js的基础,如需要补充Node.js知识的,请自行百度. Hexo是在Node.js框架下的一个项目,利用Node.js提供的强大功能,完成从Markdown到 ...

  2. Jupyter自定义设置详解

    今天专门花时间总结梳理一下jupyter的一些高级设置,jupyter我已经介绍过一次基本内容了,Setup and Linux | James Chen's Blogs,尤其是如何在服务器运行jup ...

  3. 绘制基本图形和线型(StrokeStyle)的设置详解

    绘制基本图形和线型(StrokeStyle)的设置详解 目前,在博客园上,相对写得比较好的两个关于Direct2D的教程系列,分别是万一的Direct2D系列和zdd的Direct2D系列.有兴趣的网 ...

  4. JS中的函数节流throttle详解和优化

    JS中的函数节流throttle详解和优化在前端开发中,有时会为页面绑定resize事件,或者为一个页面元素绑定拖拽事件(mousemove),这种事件有一个特点,在一个正常的操作中,有可能在一个短的 ...

  5. my.cnf 详解与优化【转】

    MySQL配置文件my.cnf 例子最详细翻译,可以保存做笔记用. #BEGIN CONFIG INFO#DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大#T ...

  6. apache配置文件详解与优化

    apache配置文件详解与优化 一.总结 一句话总结:结合apache配置文件中的英文说明和配置详解一起看 1.apache模块配置用的什么标签? IfModule 例如: <IfModule ...

  7. tomcat常用配置详解和优化方法

    tomcat常用配置详解和优化方法 参考: http://blog.csdn.net/zj52hm/article/details/51980194 http://blog.csdn.net/wuli ...

  8. 【转】Eclipse Java注释模板设置详解

    Eclipse Java注释模板设置详解   设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后 ...

  9. Win7 NFS 设置详解 | X-Space

    Win7 NFS 设置详解 | X-Space Win7 NFS 设置详解

随机推荐

  1. oracle sql insert插入字符&

    最近遇到insert 语句插入&字符报弹出框,如下: sql: insert into test_ldl001 (ID, NAME) values (', '/test/test.do?act ...

  2. P5658 括号树

    P5658 括号树 题解 太菜了啥都不会写只能水5分数据 啥都不会写只能翻题解  题解大大我错了 我们手动找一下规律 我们设 w[ i ] 为从根节点到结点 i 对答案的贡献,也就是走到结点 i ,合 ...

  3. python脚本实现-excel二级统计

    pandas和SQL数据分析实战视频教程 https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2& ...

  4. Intel AI Cloud 使用

    1.申请AI Cloud A ‘training-ready’ hardware like Amazon® EC2, Intel® AI DevCloud, or a GPU-based system ...

  5. linux redis 设置密码:

    在服务器上,这里以linux服务器为例,为redis配置密码. 1.第一种方式 (当前这种linux配置redis密码的方法是一种临时的,如果redis重启之后密码就会失效,) (1)首先进入redi ...

  6. (三)Centos之安装Xshell

    Xshell就是一个远程控制Centos的软件:(用XShell比较方便,试用的都知道,界面也人性化) 详细介绍请看 百度百科 我随便百度下载了一个中文版的 给下地址  http://pan.baid ...

  7. eclipse spring3.X redis 整合-配置

    花了一天时间折腾redis的配置 用到的jar spring 3.1.1 aopalliance-1.0.jar commons-pool2-2.3.jar jedis-2.7.2.jar sprin ...

  8. 编写高质量iOS代码与OS X代码的effective方法(小结)

    一.熟悉OC: 了解OC的起源: OC和C++,Java等面向对象语言类似,不过有很方面差别.因为该语言使用  消息结构而非函数调用. 消息结构和函数调用的区别:前者是在其运行时所应执行的代码由运行环 ...

  9. Azure DevOps的variable group实现array和hashtable参数的传递

    Azure Devops中的variable group建议或者只能(?)添加string类型的value.基于此我们想在variable group实现array或者hashtable的传递的核心思 ...

  10. laravel的ORM转为原生sql

    注:mysql测试成功,mongoDB测试失败//将laravel的ORM转为原生sql $SubProfits为laravel的ORM对象 $SubProfits = model::where('i ...