假设我有一个包含多个图例的ggplot.
mov <- subset(movies, length != "")
(p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
geom_point()
)
Run Code Online (Sandbox Code Playgroud)
我可以像这样关掉所有传说的显示:
(p1 <- p0 + theme(legend.position = "none"))
Run Code Online (Sandbox Code Playgroud)
传递show_guide = FALSE
给geom_point
(根据这个问题)关闭形状图例.
(p2 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
geom_point(show_guide = FALSE)
)
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要关闭颜色图例呢?似乎没有办法告诉show_guide
哪个图例应用其行为.show_guide
对于尺度或美学没有任何争论.
(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
Run Code Online (Sandbox Code Playgroud)
这个问题表明现代(自ggplot2 v0.9.2)控制传说的方式是与guides
功能.
我希望能够做类似的事情
p0 + guides(
colour = guide_legend(show = FALSE)
)
Run Code Online (Sandbox Code Playgroud)
但guide_legend
没有展示参数.
如何指定显示哪些图例?
Did*_*rts 268
您可以使用guide=FALSE
in scale_..._...()
来压制图例.
对于您的示例,您应该使用scale_colour_continuous()
因为length
是连续变量(不是离散的).
(p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
scale_colour_continuous(guide = FALSE) +
geom_point()
)
Run Code Online (Sandbox Code Playgroud)
或者使用功能,guides()
您应该设置FALSE
为该元素/你不希望出现的传说,例如审美,fill
,shape
,colour
.
p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
geom_point()
p0+guides(colour=FALSE)
Run Code Online (Sandbox Code Playgroud)
两个提供的解决方案都在新ggplot2
版本2.0.0中工作,但movies
此库中不再存在数据集.相反,您必须使用新包ggplot2movies
来检查这些解决方案.
library(ggplot2movies)
data(movies)
mov <- subset(movies, length != "")
Run Code Online (Sandbox Code Playgroud)