在顶部和右侧添加轴刻度标记到ggplot? [英] Add axis tick-marks on top and to the right to a ggplot?

查看:362
本文介绍了在顶部和右侧添加轴刻度标记到ggplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

base 图形中,我们可以通过使用 axis 以及 side 参数:

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

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

解决方案

这反映了轴刻度标记(使用



编辑2016年4月18日 <$ c 功能),并将刻度线放在绘图面板中。 $ c> axis.ticks.margin 已弃用。改为使用文字边距。



编辑19 Mrch 2015 :更好地定位勾号标记

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

#Get
p = ggplot(data.frame(x = 1:10,y = 1:10),aes(x,y))+
geom_point()+
theme_bw()+
主题(panel.grid = element_blank(),
axis.ticks.length =单位(-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)))

#将图转换为grob
gt< - ggplotGrob(p)

#获取布局中的面板位置
面板< -c(subset(gt $ layout,name ==panel,se = t:r))

##对于底部轴
#获取布局中底部轴的行号
rn< - 其中($ $ layout $ name ==axis-b)

#提取轴(仅限刻度线)
axis.grob< - gt $ grobs [[rn]]
axisb< - axis.grob $ children [[2]]#两个孩子 - 获得第二个
axisb#注意:两个grobs - 刻度标记和文本

#获得勾号
xaxis = axisb $ grobs [[1]]#注意:首先勾选标记
xaxis $ y = xaxis $ y - 单位(0.25,cm )#将它们放置在面板中

#将一个新行添加到gt,并将修改后的xaxis grob插入到新行中。
gt < - gtable_add_rows(gt,单元(0,lines),panel $ t-1)
gt < - gtable_add_grob(gt,xaxis,l = panel $ l,t = panel $ t,r = panel $ r,name =ticks)

##重复左轴
#获取布局中左轴的行号
面板< -c(子集(gt $布局,名称==面板,se = t:r))
rn< - 其中(GT $ layout $ name ==axis-l)

#提取轴(刻度线和轴文本)
axis.grob< - gt $ grobs [[rn]]
axisl< - axis.grob $ children [ [2]]#两个孩子 - 获得第二个
axisl#注意:两个grobs - 文本和刻度标记

#获取刻度标记
yaxis = axisl $ grobs [[[ 2]]#注意:第二个刻度线为
yaxis $ x = yaxis $ x - 单位(0.25,cm)#将它们放置在面板内

#添加一个新列到gt ,并将修改后的yaxis grob插入新列。
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)

#关闭
gt $布局[gt $ layout $ name ==ticks,] $ clip =off

#绘制
grid.draw(gt)


In base graphics we can easily add tick-marks at different sides of the plot by using axis and the side argument:

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

How can I mirror x and y axis ticks on the top and right in ggplot2? There is annotation_logticks which can do this, but only seems to work well for logarithmic scales (as the name of the function applies). For linear scales, is there a similarly easy way?

解决方案

This mirrors the axis tick marks (using gtable functions), and puts the tick marks inside the plot panel.

EDIT 18 April 2016 axis.ticks.margin is deprecated. Use text margins instead.

EDIT 19 Mrch 2015: Better positioning of the tick marks

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)

这篇关于在顶部和右侧添加轴刻度标记到ggplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆