简体   繁体   English

区别主题图例位置和show.legend

[英]Difference theme legend position and show.legend

I am studying ggplot2 Elegant Graphics for Data Analysis.我正在研究用于数据分析的ggplot2优雅图形。 What difference both code are?两个代码有什么区别?

ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point(show.legend = FALSE) + 
  directlabels::geom_dl(aes(label = class), method = "smart.grid")


ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point() + 
  theme(legend.position = "none") +
  directlabels::geom_dl(aes(label = class), method = "smart.grid")

both create this plot都创造了这个情节

在此处输入图片说明

I wonder when I use show.legend = FALSE我想知道我show.legend = FALSE时候使用show.legend = FALSE

From ?geom_point?geom_point

show.legend: logical. show.legend:合乎逻辑。 Should this layer be included in the legends?这个层应该包含在图例中吗? 'NA', the default, includes if any aesthetics are mapped. 'NA' 是默认值,包括是否映射了任何美学。 'FALSE' never includes, and 'TRUE' always includes. 'FALSE' 从不包括,而 'TRUE' 总是包括。 It can also be a named logical vector to finely select the aesthetics to display.它也可以是一个命名的逻辑向量,以精细选择要显示的美学。

From ?theme?theme

legend.position: the position of legends ("none", "left", "right", "bottom", "top", or two-element numeric vector) legend.position:图例的位置(“none”、“left”、“right”、“bottom”、“top”或二元数值向量)

theme(legend.position = "none") disables all legends, geom_point(..., show.legends = FALSE) disables the legend for this (ie the geom_point ) layer. theme(legend.position = "none")禁用所有图例, geom_point(..., show.legends = FALSE)禁用此层(即geom_point )的geom_point


For example例如

ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point(show.legend = FALSE) +
  geom_label(aes(label = class)) + 
  directlabels::geom_dl(aes(label = class), method = "smart.grid")

shows no legend for geom_point , but does show a legend for geom_label .不显示geom_point ,但显示geom_label的图例。

在此处输入图片说明


On the other hand,另一方面,

ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point() +
  geom_label(aes(label = class)) + 
  theme(legend.position = "none") +
  directlabels::geom_dl(aes(label = class), method = "smart.grid")

disables all legends.禁用所有图例。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM