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

学习了ggplot2|详解八大基本绘图要素后,就可以根据自己的需要绘制图形。前面也给出了一些ggplot2绘制生信分析基本图形的例子

pheatmap|暴雨暂歇,“热图”来袭!!!

ggplot2-plotly|让你的火山图“活”过来

ggplot2|扩展包从0开始绘制雷达图

ggplot2| 绘制KEGG气泡图

ggplot2|绘制GO富集柱形图

ggplot2|从0开始绘制PCA图

ggplot2|ggpubr进行“paper”组图合并

本文将介绍一些对legend的细节操作来完成图形的“精雕细琢”。

载入R包和数据

mtcars数据集作为示例数据

library(ggplot2)#查看数据集
head(mtcars)
# 将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))

aaarticlea/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="img" />

更改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"))

aaarticlea/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="img" />

设置 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')

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 = "")

设置multiple guides顺序

使用 guide_legend() 参数:

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

最新文章

  1. js 日期
  2. 快速替换dll命名空间
  3. python对象数据的读写权限
  4. Windows系统下安装Beautiful Soup4的步骤和方法
  5. oracle 外部表
  6. 非常不错的ASP操作数据库类,支持多数据库MSSQL,ACCESS,ORACLE,MYSQL等
  7. Oracle EBS-SQL (PO-10):检查过期采购未接收订单.sql
  8. 查GDI对象泄露的利器:GDIView
  9. 在ubuntu下使用minicom连接交换机/路由器Console口
  10. 提高SQL查询效率
  11. python的学习笔记
  12. 解决Navicat连接MySQL总是报错1251的方法
  13. lua qt測試成功
  14. 删除n天前的所有目录和文件
  15. django实现分页
  16. python enumrate使用
  17. 从底层谈WebGIS 原理设计与实现(五):WebGIS中通过行列号来换算出多种瓦片的URL 之在线地图
  18. Latex 附录生成方法-附使用的一些tips
  19. 20155202 实验四 Android开发基础
  20. 经典SQL面试题(转)

热门文章

  1. JSP之BBS论坛网站的创建
  2. C语言中的函数与数学上的函数很类似
  3. android_layout_linearlayout(一)
  4. uSID:SRv6新范式
  5. MyBatis 使用枚举或其他对象
  6. [原创]Floodlight安装
  7. HDFS读写数据流程
  8. TinycoreLinux的安装使用
  9. 最短代码实现包含100个key的字典,且每个value值不同
  10. Codeforces Round #565 (Div. 3)