【问题标题】:Add axis tick-marks on top and to the right to a ggplot?在ggplot的顶部和右侧添加轴刻度线?
【发布时间】:2015-05-11 00:31:53
【问题描述】:

base 图形中,我们可以使用axisside 参数轻松地在绘图的不同侧添加刻度线:

par(tck = 0.025)
plot(1:10)
axis(side = 3, labels = FALSE)
axis(side = 4, labels = FALSE)

如何在ggplot2 的顶部和右侧镜像 x 和 y 轴刻度?有annotation_logticks 可以做到这一点,但似乎只适用于对数刻度(因为函数的名称适用)。对于线性秤,有没有类似的简单方法?

【问题讨论】:

  • 容易吗?可能是not

标签: r ggplot2


【解决方案1】:

ggplot2 2.2.0,您可以在scale_ 中使用sec.axis 参数。

dup_axis 是创建辅助轴的简写,它是主轴的副本,有效地镜像主轴。

ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point() +
  scale_x_continuous(sec.axis = dup_axis()) +
  scale_y_continuous(sec.axis = dup_axis())

【讨论】:

  • 很棒的答案,但不幸的是,当您在 x 轴上有离散变量时,它不起作用。参数sec.axisscale_x_discrete 中不可用。
【解决方案2】:

这会镜像轴刻度线(使用gtable 函数),并将刻度线放在绘图面板内。

2016 年 4 月 18 日编辑 axis.ticks.margin 已弃用。改为使用文本边距。

EDIT 19 Mrch 2015:更好地定位刻度

library(ggplot2) # v2.1.0
library(gtable)  # v0.2.0
library(grid)

# Get a plot
p = ggplot(data.frame(x = 1:10, y = 1:10), aes(x,y)) + 
   geom_point() +
   theme_bw() +
   theme(panel.grid = element_blank(),
          axis.ticks.length=unit(-0.25, "cm"),
          axis.text.x = element_text(margin = margin(t = .5, unit = "cm")),
          axis.text.y = element_text(margin = margin(r = .5, unit = "cm")))

# Convert the plot to a grob
gt <- ggplotGrob(p)

# Get the position of the panel in the layout
panel <-c(subset(gt$layout, name=="panel", se=t:r))

## For the bottom axis
# Get the row number of the bottom axis in the layout
rn <- which(gt$layout$name == "axis-b")

# Extract the axis (tick marks only)
axis.grob <- gt$grobs[[rn]]
axisb <- axis.grob$children[[2]]  # Two children - get the second
axisb  # Note: two grobs - tick marks and text

# Get the tick marks
xaxis = axisb$grobs[[1]]  # NOTE: tick marks first
xaxis$y = xaxis$y - unit(0.25, "cm")  # Position them inside the panel

# Add a new row to gt, and insert the revised xaxis grob into the new row.
gt <- gtable_add_rows(gt, unit(0, "lines"), panel$t-1)
gt <- gtable_add_grob(gt, xaxis, l = panel$l, t = panel$t, r = panel$r, name = "ticks")

## Repeat for the left axis
# Get the row number of the left axis in the layout
panel <-c(subset(gt$layout, name=="panel", se=t:r))
rn <- which(gt$layout$name == "axis-l")

# Extract the axis (tick marks and axis text)
axis.grob <- gt$grobs[[rn]]
axisl <- axis.grob$children[[2]]  # Two children - get the second
axisl  # Note: two grobs -  text and tick marks

# Get the tick marks
yaxis = axisl$grobs[[2]] # NOTE: tick marks second
yaxis$x = yaxis$x - unit(0.25, "cm") # Position them inside the panel

# Add a new column to gt, and insert the revised yaxis grob into the new column.
gt <- gtable_add_cols(gt, unit(0, "lines"), panel$r)
gt <- gtable_add_grob(gt, yaxis, t = panel$t, l = panel$r+1, name = "ticks")

# Turn clipping off
gt$layout[gt$layout$name == "ticks", ]$clip = "off"

# Draw it
grid.draw(gt)

【讨论】:

  • 当我尝试重现此代码时,我得到了左侧和底部刻度的一半在轴外的一半内,并且我收到了警告:警告消息:axis.ticks.margin 已弃用。请改为设置axis.textmargin 属性。
  • @BhishanPoudel 应该修复。
  • @SandyMuspratt 太棒了,您的代码适用于单个情节。有没有办法让它也适用于方面?
  • @thiagovelso 抱歉耽搁了。很高兴看到你有答案。
【解决方案3】:

这并不能解决离散问题,但辅助轴不必与主轴重复。所以在“side=4”上,我可以对“side=2”轴进行转换。

在此处查看更多信息: https://ggplot2.tidyverse.org/reference/sec_axis.html

例如,scale_y_continuous(sec.axis = sec_axis(~ . + 10))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2016-06-03
    • 1970-01-01
    相关资源
    最近更新 更多