向 R 中的构面图添加刻度线
Add tick marks to facet plots in R
我想为刻面图添加刻度线。
例如,像这样的分面图:
require(ggplot2)
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme_minimal() +
geom_hline(yintercept=10, linetype="solid") +
geom_vline(xintercept=1.0, linetype="solid") +
scale_x_continuous(limits=c(1,8), expand =c(0,0),
breaks = rep(2:7)) +
scale_y_continuous(limits = c(10, 50), expand = c(0,0),
breaks = c(20,30,40)) +
facet_wrap(~cyl) +
theme(panel.margin.x=unit(1.2, "lines"),
panel.margin.y=unit(1.2, "lines"))
p
我希望添加刻度线,使其看起来像这样(请注意,虽然刻度线已添加到所有面,但刻度标签仅显示在左侧和底部面):
我试过使用 annotation_custom
函数将线条添加到轴上。但是我的尝试没有用。
require(gridExtra)
require(grid)
x.breaks = rep(2:7)
y.breaks = c(20,30,40)
for (i in 1:length(x.breaks)) {
p = p + annotation_custom(grob = linesGrob(y = unit(c(0, 5), "npc"),
gp=gpar(col= "black")),
xmin = x.breaks[i],
xmax = x.breaks[i],
ymin = 8.69,
ymax = 8.89)
}
for (i in 1:length(y.breaks)) {
p = p + annotation_custom(grob = linesGrob(x = unit(c(0, 5), "npc"),
gp=gpar(col= "black")),
xmin = 0.769,
xmax = 0.809,
ymin = y.breaks[i],
ymax = y.breaks[i])
}
gt = ggplotGrob(p)
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)
有人有什么建议吗?提前致谢。
这是一个使用 grid.arrange
函数和 grid
包的解决方案:
p_top = ggplot(subset(mpg, cyl %in% c(4,5)), aes(displ, hwy)) +
geom_point() +
theme_minimal() +
geom_hline(yintercept=10, linetype="solid") +
geom_vline(xintercept=1.0, linetype="solid") +
scale_x_continuous(limits=c(1,8), expand =c(0,0),
breaks = rep(2:7)) +
scale_y_continuous(limits = c(10, 50), expand = c(0,0),
breaks = c(20,30,40)) +
facet_wrap(~cyl, ncol = 2) +
theme(axis.ticks.x = element_line(), axis.text.x = element_blank()) +
labs(x=NULL, y = NULL)
p_bot = ggplot(subset(mpg, cyl %in% c(6,8)), aes(displ, hwy)) +
geom_point() +
theme_minimal() +
geom_hline(yintercept=10, linetype="solid") +
geom_vline(xintercept=1.0, linetype="solid") +
scale_x_continuous(limits=c(1,8), expand =c(0,0),
breaks = rep(2:7)) +
scale_y_continuous(limits = c(10, 50), expand = c(0,0),
breaks = c(20,30,40)) +
facet_wrap(~cyl, ncol = 2) +
theme(axis.ticks.x = element_line()) +
labs(y = NULL)
library(grid)
grid.arrange(p_top, p_bot, left = textGrob("hwy", rot = 90, vjust = 1))
这是一种削减数字的低级方法,
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme_bw() +
geom_hline(yintercept=10, linetype="solid") +
geom_vline(xintercept=1.0, linetype="solid") +
scale_x_continuous(limits=c(1,8), expand =c(0,0),
breaks = rep(2:7)) +
scale_y_continuous(limits = c(10, 50), expand = c(0,0),
breaks = c(20,30,40)) +
facet_wrap(~cyl, scales="free")
g <- ggplotGrob(p)
g$grobs$axis_b1$children[[2]][["grobs"]][[2]] <- nullGrob()
g$grobs$axis_b2$children[[2]][["grobs"]][[2]] <- nullGrob()
grid.newpage()
grid.draw(g)
也可以删除 gtable 中相应的 space。
我想为刻面图添加刻度线。 例如,像这样的分面图:
require(ggplot2)
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme_minimal() +
geom_hline(yintercept=10, linetype="solid") +
geom_vline(xintercept=1.0, linetype="solid") +
scale_x_continuous(limits=c(1,8), expand =c(0,0),
breaks = rep(2:7)) +
scale_y_continuous(limits = c(10, 50), expand = c(0,0),
breaks = c(20,30,40)) +
facet_wrap(~cyl) +
theme(panel.margin.x=unit(1.2, "lines"),
panel.margin.y=unit(1.2, "lines"))
p
我希望添加刻度线,使其看起来像这样(请注意,虽然刻度线已添加到所有面,但刻度标签仅显示在左侧和底部面):
我试过使用 annotation_custom
函数将线条添加到轴上。但是我的尝试没有用。
require(gridExtra)
require(grid)
x.breaks = rep(2:7)
y.breaks = c(20,30,40)
for (i in 1:length(x.breaks)) {
p = p + annotation_custom(grob = linesGrob(y = unit(c(0, 5), "npc"),
gp=gpar(col= "black")),
xmin = x.breaks[i],
xmax = x.breaks[i],
ymin = 8.69,
ymax = 8.89)
}
for (i in 1:length(y.breaks)) {
p = p + annotation_custom(grob = linesGrob(x = unit(c(0, 5), "npc"),
gp=gpar(col= "black")),
xmin = 0.769,
xmax = 0.809,
ymin = y.breaks[i],
ymax = y.breaks[i])
}
gt = ggplotGrob(p)
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)
有人有什么建议吗?提前致谢。
这是一个使用 grid.arrange
函数和 grid
包的解决方案:
p_top = ggplot(subset(mpg, cyl %in% c(4,5)), aes(displ, hwy)) +
geom_point() +
theme_minimal() +
geom_hline(yintercept=10, linetype="solid") +
geom_vline(xintercept=1.0, linetype="solid") +
scale_x_continuous(limits=c(1,8), expand =c(0,0),
breaks = rep(2:7)) +
scale_y_continuous(limits = c(10, 50), expand = c(0,0),
breaks = c(20,30,40)) +
facet_wrap(~cyl, ncol = 2) +
theme(axis.ticks.x = element_line(), axis.text.x = element_blank()) +
labs(x=NULL, y = NULL)
p_bot = ggplot(subset(mpg, cyl %in% c(6,8)), aes(displ, hwy)) +
geom_point() +
theme_minimal() +
geom_hline(yintercept=10, linetype="solid") +
geom_vline(xintercept=1.0, linetype="solid") +
scale_x_continuous(limits=c(1,8), expand =c(0,0),
breaks = rep(2:7)) +
scale_y_continuous(limits = c(10, 50), expand = c(0,0),
breaks = c(20,30,40)) +
facet_wrap(~cyl, ncol = 2) +
theme(axis.ticks.x = element_line()) +
labs(y = NULL)
library(grid)
grid.arrange(p_top, p_bot, left = textGrob("hwy", rot = 90, vjust = 1))
这是一种削减数字的低级方法,
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme_bw() +
geom_hline(yintercept=10, linetype="solid") +
geom_vline(xintercept=1.0, linetype="solid") +
scale_x_continuous(limits=c(1,8), expand =c(0,0),
breaks = rep(2:7)) +
scale_y_continuous(limits = c(10, 50), expand = c(0,0),
breaks = c(20,30,40)) +
facet_wrap(~cyl, scales="free")
g <- ggplotGrob(p)
g$grobs$axis_b1$children[[2]][["grobs"]][[2]] <- nullGrob()
g$grobs$axis_b2$children[[2]][["grobs"]][[2]] <- nullGrob()
grid.newpage()
grid.draw(g)
也可以删除 gtable 中相应的 space。