【问题标题】:Follow up on x-axis labels: Stagger axis labels, new feature in ggplot2跟进 x 轴标签:交错轴标签,ggplot2 中的新功能
【发布时间】:2016-09-22 23:24:28
【问题描述】:

Stagger axis labels, new feature in ggplot2

我是 R 的新手,遵循@Sandy Muspratt 对@spindoctor 的回答有点令人生畏。答案适用于 y 轴标签。我尝试了一些编辑。比如我改了:

index <- which(g$layout$name == "axis-l")  # Which grob

到:

index <- which(g$layout$name == "axis-b")  # Which grob

但 x 轴标签保持原样,未交错。您能否指出如何修改代码以使其也适用于 x 轴标签?

# Get the grob
g <- ggplotGrob(out.plot)

# Get the y axis
index <- which(g$layout$name == "axis-l")  # Which grob
yaxis <- g$grobs[[index]]   

# Get the ticks (labels and marks)
ticks <- yaxis$children[[2]]

# Get the labels
ticksL <- ticks$grobs[[1]]

# Make the edit
ticksL$children[[1]]$x <- rep(unit.c(unit(c(1,0,-1),"npc")), 27)

# Put the edited labels back into the plot
ticks$grobs[[1]] <- ticksL
yaxis$children[[2]] <- ticks
g$grobs[[index]] <- yaxis

# Make the relevant column a little wider
g$widths[3] <- unit(2.5, "cm")

# Draw the plot
grid.newpage()
grid.draw(g)

TableGrob 的输出如下:

>g
TableGrob (6 x 5) "layout": 8 grobs
  z     cells       name                                   grob
1 0 (1-6,1-5) background        rect[plot.background..rect.507]
2 3 (3-3,3-3)     axis-l    absoluteGrob[GRID.absoluteGrob.498]
3 1 (4-4,3-3)     spacer                         zeroGrob[NULL]
4 2 (3-3,4-4)      panel                  gTree[GRID.gTree.484]
5 4 (4-4,4-4)     axis-b    absoluteGrob[GRID.absoluteGrob.491]
6 5 (5-5,4-4)       xlab titleGrob[axis.title.x..titleGrob.501]
7 6 (3-3,2-2)       ylab titleGrob[axis.title.y..titleGrob.504]
8 7 (2-2,4-4)      title     zeroGrob[plot.title..zeroGrob.505]

我尝试识别相关的 x,y 轴值,但导航这个结构对我来说有点陌生。非常感谢任何避免浪费时间猜测的建议、cmets 或资源。

【问题讨论】:

  • @Sandy Muspratt 可能对此有意见,所以我试图通知他们。如果有更好的方法,请告诉我。
  • 也许@spindoctor 已经想出了如何做到这一点。

标签: r plot ggplot2


【解决方案1】:

在这种情况下,您只需要更改 height 而不是宽度。同样在editGrob 中,您需要传递y 插槽而不是x,因为您正在更改y 轴的坐标,而不是x 上的从左到右。

我保持一切不变,但将我更改的内容注释掉并将我的更改直接放在下面。

# Libraries
library(ggplot2)
library(gtable)
library(grid)
library(stringi)

# fake data
set.seed(12345)
var <- stri_rand_strings(81, 4, pattern = '[HrhEgeIdiFtf]')
var1 <- rnorm(81, mean = 175, sd = 75)
out <- data.frame(var, var1)
out$var <- factor(out$var, levels = out$var[order(out$var1, decreasing = FALSE)])

# Plot
# out.plot <- ggplot(out, aes(x = var, y = var1)) + geom_point() + coord_flip()
out.plot <- ggplot(out, aes(x = var1, y = var)) + geom_point() + coord_flip()

# Get the ggplot grob
g = ggplotGrob(out.plot)

# Get a hierarchical list of component grobs
grid.ls(grid.force(g))

# make the relevant column a little wider
# g$widths[3] = unit(2.5, "cm")
g$heights[4] = unit(1, "cm")

# The edit
g = editGrob(grid.force(g),
             gPath("axis-b", "axis", "axis", "GRID.text"),
             # x = unit(c(-1, 0, 1), "npc"), 
             y = unit(c(1, 0, -1), "npc"), ## or c(-1,0,1) etc to change order
             grep = TRUE)

# Draw the plot
grid.newpage()
grid.draw(g)

【讨论】:

  • 太棒了!我将 g$heights[4] = unit(1, "cm") 更改为 g$heights[4] = unit(2.5, "cm") 效果很好!我还使用了 y = unit(c(1, 0), "npc"), ##
猜你喜欢
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
相关资源
最近更新 更多