ggplot2不要图例_删除ggplot中的图例标题

本文围绕在ggplot2中移除图例标题展开。作者首次使用0.9.3版本后的ggplot2,尝试多种移除图例标题的方法均失败,如opts、theme_blank等已被弃用。最终给出解决方案,添加theme(legend.title=element_blank())即可,还提及R的Cookbook页面有图例定制的详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm trying to remove the title of a legend in ggplot2:

df

g = rep(letters[1:2], 5),

x = rnorm(10),

y = rnorm(10)

)

library(ggplot2)

ggplot(df, aes(x, y, colour=g)) +

geom_line(stat="identity") +

theme(legend.position="bottom")

I've seen this question and none of the solutions there seem to work for me. Most give an error about how opts is deprecated and to use theme instead. I've also tried various versions of theme(legend.title=NULL), theme(legend.title=""), theme(legend.title=element_blank), etc. Typical error messages are:

'opts' is deprecated. Use 'theme' instead. (Deprecated; last used in version 0.9.1)

'theme_blank' is deprecated. Use 'element_blank' instead. (Deprecated; last used in version 0.9.1)

I'm using ggplot2 for the first time since version 0.9.3 was released and I'm finding it difficult to navigate some of the changes...

解决方案

You were almost there : just add theme(legend.title=element_blank())

ggplot(df, aes(x, y, colour=g)) +

geom_line(stat="identity") +

theme(legend.position="bottom") +

theme(legend.title=element_blank())

This page on Cookbook for R gives plenty of details on how to customize legends.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值