首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言之可视化①⑨子ggplot2中的图例修改

R语言之可视化①⑨子ggplot2中的图例修改

作者头像
用户1359560
发布2019-02-22 15:18:59
4.6K0
发布2019-02-22 15:18:59
举报
文章被收录于专栏:生信小驿站生信小驿站

正文

自定义ggplot2图的图例。这里我们要修改非数据组件,通常通过theme()命令来完成。 此页面受到ggplot2(?theme)帮助页面的强烈启发。 另外,请访问非常强大的ggplot2文档以获取更多信息。我们从mtcars数据集和默认图例开始:

library(ggplot2)
basic=ggplot(mtcars, aes(wt, mpg, colour = factor(cyl), shape = factor(vs) )) +
  geom_point()
basic

image.png

  • 修改图例中的分类名字
basic+labs(
  colour = "name1",
  shape = "name2"
)

image.png

  • 删除图例中的一类
basic + guides(shape=FALSE)

image.png

修改图例位置

要删除图例,请在theme()中使用legend.position =“none”。 要更改其位置,但在图表外使用bottom,left,right或top。 或者使用0和1之间的相对坐标放置在图中。

#1: no legend
basic + theme(legend.position = "none")
#2: around the plot
basic + theme(legend.position = "bottom")
#3: inside the plot
basic + theme(
    legend.position = c(.95, .95),
    legend.justification = c("right", "top"),
    legend.box.just = "right",
    legend.margin = margin(6, 6, 6, 6)
    )

一些自定义图例的测试

# 图例被红框包裹
basic + theme(
    legend.box.background = element_rect(color="red", size=2),
    legend.box.margin = margin(116, 6, 6, 6)
)
# 图例中指示图标的边框设置
basic + theme(legend.key = element_rect(fill = "white", colour = "black"))
#图例中的字体设置
basic + theme(legend.text = element_text(size = 8, colour = "red"))
# 设置图例中的标题
basic + theme(legend.title = element_text(face = "bold"))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.01.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 正文
    • 修改图例位置
      • 一些自定义图例的测试
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档