ggplot2 |legend参数设置,图形精雕细琢

简介: ggplot2 |legend参数设置,图形精雕细琢

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

数设置,图形精雕细琢

原创生信补给站生信补给站 2019-08-19 23:30

收录于合集#R-ggplot2可视化23个

学习了ggplot2|详解八大基本绘图要素后,就可以根据自己的需要绘制图形。前面也给出了一些ggplot2绘制生信分析基本图形的例子pheatmap|暴雨暂歇,“热图”来袭!!!ggplot2-plotly|让你的火山图“活”过来ggplot2|扩展包从0开始绘制雷达图ggplot2| 绘制KEGG气泡图ggplot2|绘制GO富集柱形图ggplot2|从0开始绘制PCA图ggplot2|ggpubr进行“paper”组图合并,本文将介绍一些对legend的细节操作来完成图形的“精雕细琢”。


载入R包和数据

mtcars数据集作为示例数据
library(ggplot2)
#查看数据集
head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
# 将cyl gear变量转为因子变量
mtcars$cyl<-as.factor(mtcars$cyl)
mtcars$gear <- as.factor(mtcars$gear)

绘制基本图形

#gear为横坐标,对mpg做箱线图,gear填充颜色

p <- ggplot(mtcars, aes(x=gear, y=mpg, fill=gear)) + geom_boxplot()

p

设置 legend position

使用 theme() 参数设置legend位置

# 可选参数为“left”,“top”, “right”, “bottom”.

p + theme(legend.position="bottom")

# 数值向量 c(x,y) 调控位置

p + theme(legend.position = c(0.8, 0.2))

更改legend 的title , font styles

# legend title
p + theme(legend.title = element_text(colour="blue", size=10,
                                     face="bold"))
# legend labels
p + theme(legend.text = element_text(colour="blue", size=10,
                                    face="bold"))


设置 legend 背景色

#fill设置legend box背景色,colour设置边框颜色

p+theme(legend.background=element_rect(fill="lightblue",

                                size=0.5, linetype="solid",

                                colour="darkblue"))

设置legend items顺序

scale_x_discrete自定义设置顺序

p + scale_x_discrete(limits=c("3", "5", "4"))

去除legend

# 去除legend title
p + theme(legend.title = element_blank())
# 去除整个legend
p + theme(legend.position='none')

image.png


guides 设置specific aesthetic

使用guides()参数来设置或移除特定的美学映射(fill, color, size, shape等).

因子变量cyl和gear映射为点图的颜色和形状,qsec决定点的大小。

更多用法可参考   链接

p <- ggplot(data = mtcars,
   aes(x=mpg, y=wt, color=cyl, size=qsec, shape=gear))+
   geom_point()
# 不设定specific aesthetic时候
p

设置多个legend的位置

# 更改 legend position
p +theme(legend.position="bottom")
# Horizontal legend box
p +theme(legend.position="bottom", legend.box = "")

image.png

设置multiple guides顺序

The function guide_legend() is used :
p+guides(color = guide_legend(order=1),
        size = guide_legend(order=2),
        shape = guide_legend(order=3))

去除particular aesthetic

通过设置FALSE,可不展示对应的legend

p+guides(color = FALSE)

也可以使用scale_xx.函数去掉特定的legend

# Remove legend for the point shape
p+scale_shape(guide=FALSE)
# Remove legend for size
p +scale_size(guide=FALSE)
# Remove legend for color
p + scale_color_manual(values=c('#999999','#E69F00','#56B4E9'),
                      guide=FALSE)


通过以上参数的设置即完成对所绘制图形的legend的细节修改,得到自己所需要的图形。


参考资料:http://www.sthda.com/english/wiki/ggplot2-legend-easy-steps-to-change-the-position-and-the-appearance-of-a-graph-legend-in-r-software

相关文章
|
编译器 开发工具 git
【Git异常】You are in ‘detached HEAD‘ state, which means that you‘re not on any branch Checkout a branch
【Git异常】You are in ‘detached HEAD‘ state, which means that you‘re not on any branch Checkout a branch
601 0
|
Python
Pycharm一直卡在connecting to console的解决办法[图文步骤]
之前因为重新装了电脑系统导致有些开发软件因为不是安装在C盘的,所以没有卸载但有些环境被改变了,所以使用不正常,今天在使用pycharm的时候,打开出现了connecting to console,并且一直卡在这里
2898 0
Pycharm一直卡在connecting to console的解决办法[图文步骤]
|
8月前
|
DataWorks 监控 数据建模
DataWorks产品体验评测
DataWorks产品体验评测
|
3月前
|
存储 安全 Java
String StringBuffer StringBuilder 区别详解与对比分析
本文详细解析了Java中String、StringBuffer和StringBuilder的区别,从可变性、线程安全性和性能三个方面进行对比,并结合具体应用场景分析了三者的适用范围。通过性能测试示例展示了它们在字符串拼接时的效率差异,同时提供了实际代码案例帮助理解。总结指出,String适合少量操作或线程安全场景,StringBuffer适用于多线程环境,而StringBuilder则在单线程下性能最优。开发者应根据需求选择合适的类以优化程序性能。文末还附有相关面试资料供参考。
461 2
|
XML 搜索推荐 API
通义千问API:让大模型使用各种工具
本章我们将通过一个简单的例子,揭示基于LangChain的Agent开发的秘密,从而了解如何扩展大模型的能力。
通义千问API:让大模型使用各种工具
|
12月前
|
存储 缓存 自然语言处理
深度解析ElasticSearch:构建高效搜索与分析的基石
【9月更文挑战第8天】在数据爆炸的时代,如何快速、准确地从海量数据中检索出有价值的信息成为了企业面临的重要挑战。ElasticSearch,作为一款基于Lucene的开源分布式搜索和分析引擎,凭借其强大的实时搜索、分析和扩展能力,成为了众多企业的首选。本文将深入解析ElasticSearch的核心原理、架构设计及优化实践,帮助读者全面理解这一强大的工具。
545 8
|
机器学习/深度学习 人工智能 供应链
AI在各行业的具体应用与未来展望
人工智能(Artificial Intelligence, AI)作为一项颠覆性技术,正在逐步改变我们的生活和工作方式。从语音助手到自动驾驶汽车,AI的应用已经深入到各个领域。本文将详细探讨AI在不同行业中的具体应用,以及未来可能的发展方向。
2996 6
|
Shell Linux 开发工具
Anaconda安装后报错 -bash: conda: command not found 如何处理
【6月更文挑战第26天】Anaconda安装后报错 -bash: conda: command not found 如何处理
3421 4
基因组组装:Hifiasm 使用教程
基因组组装:Hifiasm 使用教程
|
索引 Python 数据处理
【Python Numpy教程】切片和索引
【Python Numpy教程】切片和索引
371 0
【Python Numpy教程】切片和索引