有没有办法去除ggplot2中图例的边框?

发布于 2024-08-20 19:15:15 字数 443 浏览 3 评论 0原文

我正在使用 qplot 绘制函数,并且想将图例放置在图中。我曾经

opts( legend.position = c(0.7,0.7) )

将图例移动到我想要的位置。

然而,图例周围有一个白色边框,并显示在灰色背景上。

例如:

library(ggplot2)
x = c(1:20)
y = c(1:20)

p <- qplot(x,y, color = "blue")

p <- p + scale_colour_identity("Example", breaks=c("blue"), labels=c("dots"))

p <- p + opts(legend.position = c(0.6, 0.4))

print(p)

我想知道如何从图例中删除这个边框。谢谢。

I'm using qplot to plot a function and I want to position the legend within the plot. I've used

opts( legend.position = c(0.7,0.7) )

to move the legend where I want it to be.

However there is a white border around the legend and that shows up on the gray background.

For example:

library(ggplot2)
x = c(1:20)
y = c(1:20)

p <- qplot(x,y, color = "blue")

p <- p + scale_colour_identity("Example", breaks=c("blue"), labels=c("dots"))

p <- p + opts(legend.position = c(0.6, 0.4))

print(p)

I would like to know how to remove this border from the legend. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2024-08-27 19:15:15

对于较新版本的 ggplot,opts 被替换为 theme & theme_rect 被替换为 element_rect

p + theme(legend.background = element_rect(color = NA))

这将消除你的边框:

p + opts(legend.background = theme_rect(col = 0))

除了 col (适用于边框)之外的其他选项是 fill (背景)和 size (这是边框尺寸)。

希望有帮助!

祝一切顺利,

杰伊

For newer version of ggplot, opts is replaced by theme & theme_rect is replaced by element_rect

p + theme(legend.background = element_rect(color = NA))

This will get rid of your border:

p + opts(legend.background = theme_rect(col = 0))

other options in addition to col (which applies to the border) are fill (background) and size (which is the border size).

Hope that helps!

All the best,

Jay

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文