ggplot2 中小中断的较短刻度线
Shorter tick-marks for minor breaks in ggplot2
我正在尝试 duplicate/mirror 使用 ggplot2
和 gtable
库为辅助 y 轴设置不同大小的 y 轴刻度线。
我参考这个 成功地缩短了主(左)y 轴的小刻度线。
现在,我试图在次要(右)y 轴上复制或镜像它们。
我尝试了两种方法,但都没有产生预期的结果。
在第一种方法中,我应用了与左轴相同的 :
library("ggplot2")
require("scales")
library("ggthemes")
library("grid")
library("gtable")
labs = seq(0,100,10)
labs[!!((seq_along(labs)-1)%%5)] = ''
g <- ggplot(data.frame(x = 1:10, y = (1:10)^2), aes(x,y)) +
geom_point() +
scale_y_continuous(breaks = seq(0,100,10), labels = labs, sec.axis = dup_axis(name = NULL, labels = NULL)) +
theme(axis.ticks.length=unit(10, "pt"), plot.margin = margin(10, 16, 0, 12),panel.grid = element_blank())
gg <- ggplotGrob(g)
yaxisR <- gg$grobs[[which(gg$layout$name == "axis-r")]]
yaxisL <- gg$grobs[[which(gg$layout$name == "axis-l")]]
ticksR <- yaxisR$children[[2]]
ticksL <- yaxisL$children[[2]]
marksR <- ticksR$grobs[[1]]
marksL <- ticksL$grobs[[2]]
marksR$x[c(2:5,7:10)*2-1] <- unit(1, "npc") - unit(5, "pt")
marksL$x[c(2:5,7:10)*2-1] <- unit(1, "npc") - unit(5, "pt")
ticksR$grobs[[1]] <- marksR
ticksL$grobs[[2]] <- marksL
yaxisR$children[[2]] <- ticksR
yaxisL$children[[2]] <- ticksL
gg$grobs[[which(gg$layout$name == "axis-r")]] <- yaxisR
gg$grobs[[which(gg$layout$name == "axis-l")]] <- yaxisL
grid.newpage()
grid.draw(gg)
但是右轴刻度线从错误的一侧缩短了:first result
在第二种方法中,我尝试使用 来镜像主要 y 轴刻度线:
library("ggplot2")
require("scales")
library("ggthemes")
library("grid")
library("gtable")
labs = seq(0,100,10)
labs[!!((seq_along(labs)-1)%%5)] = ''
g <- ggplot(data.frame(x = 1:10, y = (1:10)^2), aes(x,y)) +
geom_point() +
scale_y_continuous(breaks = seq(0,100,10), labels = labs) +
theme(axis.ticks.length=unit(10, "pt"), plot.margin = margin(10, 16, 0, 12), panel.grid = element_blank())
gg <- ggplotGrob(g)
panel <-c(subset(gg$layout, name=="panel", se=t:r))
yaxisL <- gg$grobs[[which(gg$layout$name == "axis-l")]]
ticksL <- yaxisL$children[[2]]
marksL <- ticksL$grobs[[2]]
marksL$x[c(2:5,7:10)*2-1] <- unit(1, "npc") - unit(5, "pt")
ticksL$grobs[[2]] <- marksL
yaxisL$children[[2]] <- ticksL
gg$grobs[[which(gg$layout$name == "axis-l")]] <- yaxisL
gg <- gtable_add_cols(gg, unit(0, "lines"), panel$r)
gg <- gtable_add_grob(gg, marksL, t = panel$t, l = panel$r+1, name = "ticks")
gg$layout[gg$layout$name == "ticks", ]$clip = "off"
grid.newpage()
grid.draw(gg)
这种方式导致右轴刻度线指向错误的方向:second result
我可以对这两个代码做些什么来使两个 y 轴相同并相互镜像吗?谢谢!
我尝试了一些东西,我不确定我是否完全理解它,但这是一个解决方案:
对象 marksR$x 每条刻度线由 2 个元素组成。我认为这些是线的左右两端。这意味着,您只需添加:
marksR$x[c(2:5,7:10)*2] <- unit(0, "npc")
同时还保持
marksR$x[c(2:5,7:10)*2-1] <- unit(1, "npc") - unit(5, "pt")
哪个适合我
我正在尝试 duplicate/mirror 使用 ggplot2
和 gtable
库为辅助 y 轴设置不同大小的 y 轴刻度线。
我参考这个
在第一种方法中,我应用了与左轴相同的
library("ggplot2")
require("scales")
library("ggthemes")
library("grid")
library("gtable")
labs = seq(0,100,10)
labs[!!((seq_along(labs)-1)%%5)] = ''
g <- ggplot(data.frame(x = 1:10, y = (1:10)^2), aes(x,y)) +
geom_point() +
scale_y_continuous(breaks = seq(0,100,10), labels = labs, sec.axis = dup_axis(name = NULL, labels = NULL)) +
theme(axis.ticks.length=unit(10, "pt"), plot.margin = margin(10, 16, 0, 12),panel.grid = element_blank())
gg <- ggplotGrob(g)
yaxisR <- gg$grobs[[which(gg$layout$name == "axis-r")]]
yaxisL <- gg$grobs[[which(gg$layout$name == "axis-l")]]
ticksR <- yaxisR$children[[2]]
ticksL <- yaxisL$children[[2]]
marksR <- ticksR$grobs[[1]]
marksL <- ticksL$grobs[[2]]
marksR$x[c(2:5,7:10)*2-1] <- unit(1, "npc") - unit(5, "pt")
marksL$x[c(2:5,7:10)*2-1] <- unit(1, "npc") - unit(5, "pt")
ticksR$grobs[[1]] <- marksR
ticksL$grobs[[2]] <- marksL
yaxisR$children[[2]] <- ticksR
yaxisL$children[[2]] <- ticksL
gg$grobs[[which(gg$layout$name == "axis-r")]] <- yaxisR
gg$grobs[[which(gg$layout$name == "axis-l")]] <- yaxisL
grid.newpage()
grid.draw(gg)
但是右轴刻度线从错误的一侧缩短了:first result
在第二种方法中,我尝试使用
library("ggplot2")
require("scales")
library("ggthemes")
library("grid")
library("gtable")
labs = seq(0,100,10)
labs[!!((seq_along(labs)-1)%%5)] = ''
g <- ggplot(data.frame(x = 1:10, y = (1:10)^2), aes(x,y)) +
geom_point() +
scale_y_continuous(breaks = seq(0,100,10), labels = labs) +
theme(axis.ticks.length=unit(10, "pt"), plot.margin = margin(10, 16, 0, 12), panel.grid = element_blank())
gg <- ggplotGrob(g)
panel <-c(subset(gg$layout, name=="panel", se=t:r))
yaxisL <- gg$grobs[[which(gg$layout$name == "axis-l")]]
ticksL <- yaxisL$children[[2]]
marksL <- ticksL$grobs[[2]]
marksL$x[c(2:5,7:10)*2-1] <- unit(1, "npc") - unit(5, "pt")
ticksL$grobs[[2]] <- marksL
yaxisL$children[[2]] <- ticksL
gg$grobs[[which(gg$layout$name == "axis-l")]] <- yaxisL
gg <- gtable_add_cols(gg, unit(0, "lines"), panel$r)
gg <- gtable_add_grob(gg, marksL, t = panel$t, l = panel$r+1, name = "ticks")
gg$layout[gg$layout$name == "ticks", ]$clip = "off"
grid.newpage()
grid.draw(gg)
这种方式导致右轴刻度线指向错误的方向:second result
我可以对这两个代码做些什么来使两个 y 轴相同并相互镜像吗?谢谢!
我尝试了一些东西,我不确定我是否完全理解它,但这是一个解决方案:
对象 marksR$x 每条刻度线由 2 个元素组成。我认为这些是线的左右两端。这意味着,您只需添加:
marksR$x[c(2:5,7:10)*2] <- unit(0, "npc")
同时还保持
marksR$x[c(2:5,7:10)*2-1] <- unit(1, "npc") - unit(5, "pt")
哪个适合我