当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R ggplot2 annotation_logticks 注释:记录刻度线


此注释添加了间距递减的对数刻度线。这些刻度线可能只对以 10 为基数的情况有意义。

用法

annotation_logticks(
  base = 10,
  sides = "bl",
  outside = FALSE,
  scaled = TRUE,
  short = unit(0.1, "cm"),
  mid = unit(0.2, "cm"),
  long = unit(0.3, "cm"),
  colour = "black",
  size = 0.5,
  linetype = 1,
  alpha = 1,
  color = NULL,
  ...
)

参数

base

日志的基数(默认10)

sides

控制对数刻度出现在图的哪一侧的字符串。它可以设置为包含任何 "trbl" 的字符串,用于顶部、右侧、底部和左侧。

outside

控制是否将日志刻度移动到绘图区域之外的逻辑。默认关闭(FALSE)。您还需要使用coord_cartesian(clip = "off")。请参阅示例。

scaled

数据已经log-scaled了吗?当数据已使用 log10() 转换或使用 scale_y_log10() 时,这应该是 TRUE (默认)。使用 coord_trans(y = "log10") 时应为 FALSE

short

指定短刻度线长度的 grid::unit() 对象

mid

指定中间刻度线长度的 grid::unit() 对象。以 10 为基数,这些是 "5" 刻度。

long

指定长刻度线长度的 grid::unit() 对象。以 10 为基数,这些是"1"(或"10")刻度。

colour

刻度线的颜色。

size

刻度线的厚度,以毫米为单位。

linetype

刻度线的线型( soliddashed 等)

alpha

刻度线的透明度。

color

colour 的别名。

...

传递到层的其他参数

也可以看看

scale_y_continuous()scale_y_log10() 用于对数刻度转换。

coord_trans() 用于日志坐标转换。

例子

# Make a log-log plot (without log ticks)
a <- ggplot(msleep, aes(bodywt, brainwt)) +
 geom_point(na.rm = TRUE) +
 scale_x_log10(
   breaks = scales::trans_breaks("log10", function(x) 10^x),
   labels = scales::trans_format("log10", scales::math_format(10^.x))
 ) +
 scale_y_log10(
   breaks = scales::trans_breaks("log10", function(x) 10^x),
   labels = scales::trans_format("log10", scales::math_format(10^.x))
 ) +
 theme_bw()

a + annotation_logticks()                # Default: log ticks on bottom and left

a + annotation_logticks(sides = "lr")    # Log ticks for y, on left and right

a + annotation_logticks(sides = "trbl")  # All four sides


a + annotation_logticks(sides = "lr", outside = TRUE) +
 coord_cartesian(clip = "off")  # Ticks outside plot


# Hide the minor grid lines because they don't align with the ticks
a + annotation_logticks(sides = "trbl") + theme(panel.grid.minor = element_blank())


# Another way to get the same results as 'a' above: log-transform the data before
# plotting it. Also hide the minor grid lines.
b <- ggplot(msleep, aes(log10(bodywt), log10(brainwt))) +
 geom_point(na.rm = TRUE) +
 scale_x_continuous(name = "body", labels = scales::math_format(10^.x)) +
 scale_y_continuous(name = "brain", labels = scales::math_format(10^.x)) +
 theme_bw() + theme(panel.grid.minor = element_blank())

b + annotation_logticks()


# Using a coordinate transform requires scaled = FALSE
t <- ggplot(msleep, aes(bodywt, brainwt)) +
  geom_point() +
  coord_trans(x = "log10", y = "log10") +
  theme_bw()
t + annotation_logticks(scaled = FALSE)
#> Warning: Removed 27 rows containing missing values (`geom_point()`).


# Change the length of the ticks
a + annotation_logticks(
  short = unit(.5,"mm"),
  mid = unit(3,"mm"),
  long = unit(4,"mm")
)

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Annotation: log tick marks。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。