當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R ggplot2 guides 為每個尺度設置指南

每個比例的指南可以使用 guide 參數設置 scale-by-scale ,或者使用 guides() 一起設置。

用法

guides(...)

參數

...

name-guide 秤對列表。指南可以是字符串(即 "colorbar" 或 "legend"),也可以是對指定其他參數的指南函數(即 guide_colourbar()guide_legend() )的調用。

包含比例和參考線之間映射的列表。

也可以看看

例子

# \donttest{
# ggplot object

dat <- data.frame(x = 1:5, y = 1:5, p = 1:5, q = factor(1:5),
 r = factor(1:5))
p <-
  ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) +
  geom_point()

# without guide specification
p
#> Warning: Using size for a discrete variable is not advised.


# Show colorbar guide for colour.
# All these examples below have a same effect.

p + guides(colour = "colorbar", size = "legend", shape = "legend")
#> Warning: Using size for a discrete variable is not advised.

p + guides(colour = guide_colorbar(), size = guide_legend(),
  shape = guide_legend())
#> Warning: Using size for a discrete variable is not advised.

p +
 scale_colour_continuous(guide = "colorbar") +
 scale_size_discrete(guide = "legend") +
 scale_shape(guide = "legend")
#> Warning: Using size for a discrete variable is not advised.


 # Remove some guides
 p + guides(colour = "none")
#> Warning: Using size for a discrete variable is not advised.

 p + guides(colour = "colorbar",size = "none")
#> Warning: Using size for a discrete variable is not advised.


# Guides are integrated where possible

p +
  guides(
    colour = guide_legend("title"),
    size = guide_legend("title"),
    shape = guide_legend("title")
 )
#> Warning: Using size for a discrete variable is not advised.

# same as
g <- guide_legend("title")
p + guides(colour = g, size = g, shape = g)
#> Warning: Using size for a discrete variable is not advised.


p + theme(legend.position = "bottom")
#> Warning: Using size for a discrete variable is not advised.


# position of guides

# Set order for multiple guides
ggplot(mpg, aes(displ, cty)) +
  geom_point(aes(size = hwy, colour = cyl, shape = drv)) +
  guides(
   colour = guide_colourbar(order = 1),
   shape = guide_legend(order = 2),
   size = guide_legend(order = 3)
 )

# }
源代碼:R/guides-.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Set guides for each scale。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。