scale_x_discrete() 和 scale_y_discrete() 用于设置离散 x 和 y 尺度美观的值。为了简单地操作刻度标签和限制,您可能希望使用labs() 和lims() 代替。
用法
scale_x_discrete(..., expand = waiver(), guide = waiver(), position = "bottom")
scale_y_discrete(..., expand = waiver(), guide = waiver(), position = "left")
参数
- ...
 - 
参数传递给
discrete_scalepalette- 
调色板函数,当使用单个整数参数(比例中的级别数)调用时,返回它们应采用的值(例如
scales::hue_pal())。 breaks- 
之一:
 limits- 
之一:
- 
NULL使用默认比例值 - 
定义可能的比例值及其顺序的字符向量
 - 
接受现有(自动)值并返回新值的函数。还接受 rlang lambda 函数表示法。
 
 - 
 drop- 
是否应该从量表中省略未使用的因子水平?默认值
TRUE使用数据中出现的级别;FALSE使用因子中的所有级别。 na.translate- 
与连续尺度不同,离散尺度可以轻松显示缺失值,并且默认情况下会这样做。如果要从离散尺度中删除缺失值,请指定
na.translate = FALSE。 na.value- 
如果
na.translate = TRUE,缺失值应该显示为什么美学值?不适用于NA始终位于最右侧的位置比例。 aesthetics- 
该量表所适用的美学名称。
 scale_name- 
应用于与该比例关联的错误消息的比例名称。
 name- 
秤的名称。用作轴或图例标题。如果
waiver()(默认值),则比例名称取自用于该美学的第一个映射。如果是NULL,则图例标题将被省略。 labels- 
之一:
 super- 
用于构造比例的超类
 
 - expand
 - 
对于位置刻度,范围扩展常量的向量,用于在数据周围添加一些填充,以确保它们放置在距轴一定距离的位置。使用便捷函数
expansion()生成expand参数的值。默认情况下,对于连续变量,每侧扩展 5%,对于离散变量,每侧扩展 0.6 个单位。 - guide
 - 
用于创建指南或其名称的函数。有关详细信息,请参阅
guides()。 - position
 - 
对于位置刻度,轴的位置。
left或right表示 y 轴,top或bottom表示 x 轴。 
细节
即使使用离散位置刻度,您也可以使用连续位置 - 这允许您(例如)在条形图中的条形之间放置标签。连续位置是数字值,第一级从 1 开始,每个级别增加 1(即标签放置在整数位置)。这就是抖动发挥作用的原因。
也可以看看
其他位置刻度:scale_x_binned()、scale_x_continuous()、scale_x_date()
例子
ggplot(diamonds, aes(cut)) + geom_bar()
# \donttest{
# The discrete position scale is added automatically whenever you
# have a discrete position.
(d <- ggplot(subset(diamonds, carat > 1), aes(cut, clarity)) +
      geom_jitter())
d + scale_x_discrete("Cut")
d +
  scale_x_discrete(
    "Cut",
    labels = c(
      "Fair" = "F",
      "Good" = "G",
      "Very Good" = "VG",
      "Perfect" = "P",
      "Ideal" = "I"
    )
  )
# Use limits to adjust the which levels (and in what order)
# are displayed
d + scale_x_discrete(limits = c("Fair","Ideal"))
#> Warning: Removed 11189 rows containing missing values (`geom_point()`).
# you can also use the short hand functions xlim and ylim
d + xlim("Fair","Ideal", "Good")
#> Warning: Removed 9610 rows containing missing values (`geom_point()`).
d + ylim("I1", "IF")
#> Warning: Removed 16770 rows containing missing values (`geom_point()`).
# See ?reorder to reorder based on the values of another variable
ggplot(mpg, aes(manufacturer, cty)) +
  geom_point()
ggplot(mpg, aes(reorder(manufacturer, cty), cty)) +
  geom_point()
ggplot(mpg, aes(reorder(manufacturer, displ), cty)) +
  geom_point()
# Use abbreviate as a formatter to reduce long names
ggplot(mpg, aes(reorder(manufacturer, displ), cty)) +
  geom_point() +
  scale_x_discrete(labels = abbreviate)
# }
相关用法
- R ggplot2 scale_date 日期/时间数据的位置刻度
 - R ggplot2 scale_gradient 渐变色阶
 - R ggplot2 scale_shape 形状比例,又称字形
 - R ggplot2 scale_viridis 来自 viridisLite 的 Viridis 色标
 - R ggplot2 scale_grey 连续灰度色阶
 - R ggplot2 scale_linetype 线条图案的比例
 - R ggplot2 scale_manual 创建您自己的离散尺度
 - R ggplot2 scale_colour_discrete 离散色阶
 - R ggplot2 scale_steps 分级渐变色标
 - R ggplot2 scale_size 面积或半径比例
 - R ggplot2 scale_continuous 连续数据的位置比例(x 和 y)
 - R ggplot2 scale_binned 用于对连续数据进行装箱的位置比例(x 和 y)
 - R ggplot2 scale_alpha Alpha 透明度比例
 - R ggplot2 scale_colour_continuous 连续色标和分级色标
 - R ggplot2 scale_identity 使用不缩放的值
 - R ggplot2 scale_linewidth 线宽比例
 - R ggplot2 scale_hue 离散数据的均匀间隔颜色
 - R ggplot2 scale_brewer ColorBrewer 的连续、发散和定性色标
 - R ggplot2 stat_ellipse 计算法行数据椭圆
 - R ggplot2 stat_identity 保留数据原样
 - R ggplot2 stat_summary_2d 以二维形式进行分类和汇总(矩形和六边形)
 - R ggplot2 should_stop 在示例中用于说明何时应该发生错误。
 - R ggplot2 stat_summary 总结唯一/分箱 x 处的 y 值
 - R ggplot2 stat_sf_coordinates 从“sf”对象中提取坐标
 - R ggplot2 stat_unique 删除重复项
 
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Position scales for discrete data。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
