关闭ggplot中的一些传说 [英] Turning off some legends in a ggplot
问题描述
mov< - subset(movies,length!= )
(p0 < - ggplot(mov,aes(year,rating,color = length,shape = mpaa))+
geom_point()
)
我可以关闭所有图例的显示:
(p1 < - p0 + theme(legend.position =none))
传递 show_guide = FALSE
至 geom_point
(按照
<$> c $ c>(p2 < - ggplot(mov,aes(year,rating,color = length,shape = mpaa))+
geom_point(show_guide = FALSE)
)
但是如果我想关闭颜色图例呢?似乎没有办法告诉 show_guide
哪个图例应用其行为。
(p3) < - ggplot(mov,aes(year,rating,color = length,shape = mpaa))+
scale_colour_discrete(show_guide = FALSE)+
geom_point()
)
#discrete_scale中的错误
(p4 < - ggplot(mov,aes(year,rating,shape = mpaa))+
aes(color = length,show_guide = FALSE)+
geom_point()
)
#两个传说都是
这个问题表明,控制传说的现代(自ggplot2 v0.9.2)方式与 guides
函数。
我希望能够做到像
$ b $ (
color = guide_legend(show = FALSE)
)
$ p $ b
p0 + p>
但 guide_legend
没有show参数。
我如何指定w您可以使用 guide = FALSE
来显示它们吗?
scale _......()
来压制图例。
对于您的示例,您应该使用 scale_colour_continuous()
因为长度
是连续变量(不是离散的)。
< $ p $ (p3 < - ggplot(mov,aes(year,rating,color = length,shape = mpaa))+
scale_colour_continuous(guide = FALSE)+
geom_point()
)
或者使用函数 guides()
您应该设置 FALSE
作为您不想显示为图例的元素/美学,例如填充
,形状
,颜色
。
p0 < - ggplot(mov,aes(year,rating,color = length,shape = mpaa))+
geom_point()
p0 + (color = FALSE)
UPDATE
两个提供的解决方案都可以工新的 ggplot2
版本2.0.0但是 movies
数据集不再存在于此库中。相反,您必须使用新包 ggplot2movies
来检查这些解决方案。
library(ggplot2movies)
data(movies)
mov < - subset(movies,length!=)
Suppose I have a ggplot with more than one legend.
mov <- subset(movies, length != "")
(p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
geom_point()
)
I can turn off the display of all the legends like this:
(p1 <- p0 + theme(legend.position = "none"))
Passing show_guide = FALSE
to geom_point
(as per this question) turns off the shape legend.
(p2 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
geom_point(show_guide = FALSE)
)
But what if I want to turn off the colour legend instead? There doesn't seem to be a way of telling show_guide
which legend to apply its behaviour to. And there is no show_guide
argument for scales or aesthetics.
(p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
scale_colour_discrete(show_guide = FALSE) +
geom_point()
)
# Error in discrete_scale
(p4 <- ggplot(mov, aes(year, rating, shape = mpaa)) +
aes(colour = length, show_guide = FALSE) +
geom_point()
)
#draws both legends
This question suggests that the modern (since ggplot2 v0.9.2) way of controlling legends is with the guides
function.
I want to be able to do something like
p0 + guides(
colour = guide_legend(show = FALSE)
)
but guide_legend
doesn't have a show argument.
How do I specify which legends get displayed?
You can use guide=FALSE
in scale_..._...()
to suppress legend.
For your example you should use scale_colour_continuous()
because length
is continuous variable (not discrete).
(p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
scale_colour_continuous(guide = FALSE) +
geom_point()
)
Or using function guides()
you should set FALSE
for that element/aesthetic that you don't want to appear as legend, for example, fill
, shape
, colour
.
p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
geom_point()
p0+guides(colour=FALSE)
UPDATE
Both provided solutions work in new ggplot2
version 2.0.0 but movies
dataset is no longer present in this library. Instead you have to use new package ggplot2movies
to check those solutions.
library(ggplot2movies)
data(movies)
mov <- subset(movies, length != "")
这篇关于关闭ggplot中的一些传说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!