如何延长刻面网格ggplot中的特定刻度线? [英] How to lengthen specific tick marks in facet gridded ggplot?

查看:166
本文介绍了如何延长刻面网格ggplot中的特定刻度线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在小平面网格中具有标签的标签,我希望使用更长的刻度线.因此,我尝试了此尝试,并尝试将其调整为适用于分面网格图,例如:

I want longer tick marks for those with labels in a facet grid. So I worked through this attempt and tried to adapt it to facet gridded plots like so:

定义次要和主要的休息和标签:

range.f <- range(unique(df1$weeks))
minor.f <- 1  # every 1 week, NOTE: range.f[2] should be divisible by minor.f!
major.f <- 5  # every 5 weeks

breaks.f <- seq(range.f[1], range.f[2], minor.f)

every_nth.lt <- function (x, nth) {x[1:nth != 1] <- ""; x}
# (lite version of https://stackoverflow.com/a/34533473/6574038
# works better for me than `insert_minor()`)

labels.f <- every_nth.lt(sequence(range.f[2]), major.f)

n_minor.f <- major.f / minor.f - 1

正态图:

library(ggplot2)
p.f <- ggplot(df1, aes(weeks, births)) +
  geom_bar(stat="identity", fill="#F48024") + theme_bw() +
  scale_x_continuous(breaks=breaks.f, labels=labels.f) +
  coord_cartesian(xlim=range.f) +
  facet_wrap(year ~ .) +
  theme(panel.grid = element_blank(),
        axis.text.x = element_text(margin=margin(t=5, unit="pt")))

操作图:

g.f <- ggplotGrob(p.f)
xaxis.f <- g.f$grobs[grep("^axis-b", g.f$layout$name)]  # get x-axes
ticks.f <- do.call(c, lapply(lapply(xaxis.f, "["), 
                             function(x) x$children[[2]]))  # get ticks
marks.f <- ticks.f$grobs[[1]]  # get tick marks
# editing y-positions of tick marks
marks.f$y <- unit.c(unit.c(unit(1, "npc") - unit(6, "pt"), 
                           unit(1, "npc"), 
                           rep(unit.c(unit(1, "npc") - unit(3, "pt"), 
                                      unit(1, "npc")), n_minor.f)))

# putting tick marks back into plot
ticks.f$grobs[[1]] <- marks.f
for(i in seq_along(xaxis.f)) {
  xaxis.f[[i]]$children[[2]]$grob <- ticks.f[[i]]
}
g.f$grobs[grep("^axis-b", g.f$layout$name)] <- xaxis.f

绘制图:

library(grid)
grid.newpage()
grid.draw(g.f)

屈服:

我遵循了链接答案的所有步骤,只是将其调整为适合杂物清单的情况.但是,较长的刻度不会出现.

I followed all the steps of the linked answer, just adapted it to the situation that there are lists in the grob. But, the longer tick marks won't show up.

有人看到我做错了吗?

或者,也许还有另一种方法来延长那些带有标签的轴刻度的轴刻度吗?

Or, maybe is there another way how to lengthen the axis ticks of those axis ticks which have labels?

预期输出:

最后,所有三个图的刻度线应如下所示:

At the end the tick marks of all three plots should look like this:

数据:

tmp <- data.frame(date=as.Date(sample(1:1095, 10000, replace=TRUE), 
                               origin="2014-01-01"),
                  births=sample(0:10, 10000, replace=TRUE))
tmp$year <- factor(substr(tmp$date, 1, 4))
df1 <- aggregate(births ~ date + year, tmp, sum)
rm(tmp)  # remove tmp
df1$weeks <- as.integer(strftime(lubridate::floor_date(as.Date(df1$date, 
                                                               format="%m/%d/%Y"), 
                                                       unit="week"), "%W")) + 1

推荐答案

我确定您可以对此进行改进.我只是仔细研究了一下,然后将东西正确拉出,然后放回去.通常是通过将其与单个图进行比较,然后使其遍历一组杂项来实现的.

I'm sure you could improve upon this. I just worked through it and got things correctly pulled out, and put back in. Mostly by comparing it to a single plot, and then making it loop over a list of grobs.

范围和中断可能需要更改,因为在这里它们都是相同的,但是使用不同的x-axes,您可以适当地自定义中断.

The range and breaks may need to change, since here they're all the same, but with different x-axes you could customize the breaks appropriately.

tmp <- data.frame(date=as.Date(sample(1:1095, 10000, replace=TRUE), 
                               origin="2014-01-01"),
                  births=sample(0:10, 10000, replace=TRUE))
tmp$year <- factor(substr(tmp$date, 1, 4))
df1 <- aggregate(births ~ date + year, tmp, sum)
rm(tmp)  # remove tmp
df1$weeks <- as.integer(strftime(lubridate::floor_date(as.Date(df1$date, 
                                                               format="%m/%d/%Y"), 
                                                       unit="week"), "%W")) + 1

# breaks and labels, minor and major
range.f <- 1:(max(unique(df1$weeks)))
minor.f <- 1  # every 1 week, NOTE: range.f[2] should be divisible by minor.f!
major.f <- 5  # every 5 weeks

breaks.f <- seq(min(range.f), max(range.f), minor.f)

every_nth.lt <- function (x, nth) {x[1:nth != 1] <- ""; x}
# (lite version of https://stackoverflow.com/a/34533473/6574038)

labels.f <- every_nth.lt(range.f, major.f)

n_minor.f <- major.f / minor.f - 1

# plot
library(ggplot2)
library(grid)
p.f <- ggplot(df1, aes(weeks, births)) +
  geom_bar(stat="identity", fill="#F48024") + theme_bw() +
  scale_x_continuous(breaks=breaks.f, labels=labels.f) +
  coord_cartesian(xlim=range.f) +
  facet_wrap(year ~ .) +
  theme(panel.grid = element_blank(),
        axis.text.x = element_text(margin=margin(t=5, unit="pt")))

# manipulating plot
g.f <- ggplotGrob(p.f)
xaxis.f <- g.f$grobs[grep("^axis-b", g.f$layout$name)]  # get x-axes


ticks.f <- c()
for(i in seq_along(xaxis.f)) {
  ticks.f[[i]] <- xaxis.f[[i]]$children[[2]]
}


marks.f <- c()
for(i in seq_along(ticks.f)) {
  marks.f[[i]] <- ticks.f[[i]][1]$grobs
}



# editing y-positions of tick marks
for(i in seq_along(marks.f)) {
  marks.f[[i]][[1]]$y <- unit.c(unit.c(unit(1, "npc") - unit(6, "pt"), 
                                       unit(1, "npc"), 
                                       rep(unit.c(unit(1, "npc") - unit(3, "pt"), 
                                                  unit(1, "npc")), n_minor.f)))
}
# putting tick marks back into plot
for(i in seq_along(ticks.f)) {
  ticks.f[[i]]$grobs[[1]] <- marks.f[[i]][[1]]
}

for(i in seq_along(xaxis.f)) {
  xaxis.f[[i]]$children[[2]] <- ticks.f[[i]]
}

g.f$grobs[grep("^axis-b", g.f$layout$name)] <- xaxis.f

# plot
grid.newpage()
grid.draw(g.f)

这篇关于如何延长刻面网格ggplot中的特定刻度线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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