r语言ggplot2一夜多图_R语言之可视化①⑨子ggplot2中的图例修改

目录

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

=======================

正文

自定义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"))

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值